Postgresql https://tech.feedyourhead.at/tags/postgresql en Postgresql: Changing default template to UTF8 https://tech.feedyourhead.at/content/postgresql-changing-default-template-to-utf8 <span class="field field--name-title field--type-string field--label-hidden">Postgresql: Changing default template to UTF8</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><pre> <code> dr@tardis$ psql -U postgres psql (9.4.9) Type "help" for help. postgres=# update pg_database set datallowconn = TRUE where datname = 'template0'; UPDATE 1 postgres=# \c template0 You are now connected to database "template0". template0=# update pg_database set datistemplate = FALSE where datname = 'template1'; UPDATE 1 template0=# drop database template1; DROP DATABASE template0=# create database template1 with template = template0 encoding = 'UTF8'; CREATE DATABASE template0=# update pg_database set datistemplate = TRUE where datname = 'template1'; UPDATE 1 template0=# \c template1 You are now connected to database "template1". template1=# update pg_database set datallowconn = FALSE where datname = 'template0'; UPDATE 1 </code></pre> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/users/hoti" typeof="schema:Person" property="schema:name" datatype="">Hoti</span></span> <span class="field field--name-created field--type-created field--label-hidden">Jan 12 2017</span> <div class="field field--name-field-tagies field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class='field__items'> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/postgresql" hreflang="en">Postgresql</a></div> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/tricks" hreflang="en">Tricks</a></div> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/sysadmin" hreflang="en">Sysadmin</a></div> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/database" hreflang="en">Database</a></div> </div> </div> <section class="field field--name-comment-node-blog field--type-comment field--label-hidden comment-wrapper"> </section> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class='title comment-form__title'> <i class="fa fa-comments-o"></i> Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=224&amp;2=comment&amp;3=comment" token="itnOa09JDG9A7RDQGnY1h1Jh5Ab9KPQL1OekFVgpadc"></drupal-render-placeholder> </section> Thu, 12 Jan 2017 13:51:50 +0000 Hoti 224 at https://tech.feedyourhead.at Postgresql: Change default template0 to UTF8 encoding https://tech.feedyourhead.at/node/136 <span class="field field--name-title field--type-string field--label-hidden">Postgresql: Change default template0 to UTF8 encoding</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p><img alt="" src="http://upload.wikimedia.org/wikipedia/de/thumb/4/4b/Postgresql.svg/200px-Postgresql.svg.png" style="width: 200px; height: 206px;" /></p> <p>psqlfix.sh:<code> </code></p> <script src="https://gist.github.com/fgrehm/5436485.js"></script></div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/users/hoti" typeof="schema:Person" property="schema:name" datatype="">Hoti</span></span> <span class="field field--name-created field--type-created field--label-hidden">Dec 08 2015</span> <div class="field field--name-field-tagies field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class='field__items'> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/database" hreflang="en">Database</a></div> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/postgresql" hreflang="en">Postgresql</a></div> </div> </div> <section class="field field--name-comment-node-blog field--type-comment field--label-hidden comment-wrapper"> </section> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class='title comment-form__title'> <i class="fa fa-comments-o"></i> Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=136&amp;2=comment&amp;3=comment" token="pW0ZCGgT0OQDUyjfk-qTY-H7-_SPjqMTchJb9X71Akk"></drupal-render-placeholder> </section> Tue, 08 Dec 2015 11:41:24 +0000 Hoti 136 at https://tech.feedyourhead.at postgresql: using auto-incrementing keys https://tech.feedyourhead.at/content/postgresql-using-auto-incrementing-keys <span class="field field--name-title field--type-string field--label-hidden">postgresql: using auto-incrementing keys</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p><img alt="" src="http://upload.wikimedia.org/wikipedia/de/thumb/4/4b/Postgresql.svg/200px-Postgresql.svg.png" /></p> <p>&nbsp;</p> <p>Sometimes it's nice to have an auto-incrementing value in our database so that we don't have to generate a unique id(for example). In postgresql we can achieve this using sequences:</p> <pre> <code> CREATE SEQUENCE key_seq; CREATE TABLE sometable ( key_id INTEGER NOT NULL DEFAULT nextval('key_seq') ); ALTER SEQUENCE key_seq OWNED BY sometable.key_id; </code></pre> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/users/hoti" typeof="schema:Person" property="schema:name" datatype="">Hoti</span></span> <span class="field field--name-created field--type-created field--label-hidden">Jun 21 2015</span> <div class="field field--name-field-tagies field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class='field__items'> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/database" hreflang="en">Database</a></div> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/postgresql" hreflang="en">Postgresql</a></div> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/sysadmin" hreflang="en">Sysadmin</a></div> </div> </div> <section class="field field--name-comment-node-blog field--type-comment field--label-hidden comment-wrapper"> </section> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class='title comment-form__title'> <i class="fa fa-comments-o"></i> Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=94&amp;2=comment&amp;3=comment" token="2tmngiNG-EW7H0lXIF0A4fGR99wKDMkXAtGRvEZoY8w"></drupal-render-placeholder> </section> Sun, 21 Jun 2015 17:30:47 +0000 Hoti 94 at https://tech.feedyourhead.at postgresql: install and activate crypto-functions https://tech.feedyourhead.at/content/postgresql-install-and-activate-crypto-functions <span class="field field--name-title field--type-string field--label-hidden">postgresql: install and activate crypto-functions</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p><img alt="" src="http://upload.wikimedia.org/wikipedia/de/thumb/4/4b/Postgresql.svg/200px-Postgresql.svg.png" style="width: 200px; height: 206px;" /></p> <p>Postgresql is a pretty cool database and it even has functions for encryption.&nbsp; So if you write a user-database, you don't need to use crypto-functions from your programming language to encrypt your password. You can directly use postgresql-functions!</p> <p>Before we can do this, we have to install the required postgresql-extensions:</p> <pre> <code> apt-get install postgresql-contrib-9.4 </code></pre> <p>And then activate the extensions( I'll use here my database "columbo"):</p> <pre> <code> postgres@tardis:~$ psql -d columbo columbo=# create extension pgcrypto; CREATE EXTENSION columbo=# select * from engineer; id | username | password ----+----------+---------- 2 | pfalk | dog columbo=# update engineer set password = crypt('dog', gen_salt('bf')) WHERE id=2; UPDATE 1 columbo=# select * from engineer; id | username | password ----+----------+-------------------------------------------------------------- 2 | pfalk | $2a$06$v/HdMyli9DOhlxAsqoCQAOvapOX7k7vbQ/2ZN9A5ORLSCOk1ZWMzK columbo=# select * from engineer WHERE password = crypt('dog',password); id | username | password ----+----------+-------------------------------------------------------------- 2 | pfalk | $2a$06$v/HdMyli9DOhlxAsqoCQAOvapOX7k7vbQ/2ZN9A5ORLSCOk1ZWMzK </code></pre> <p>That's how we just created a crypted password with a generated salt(using blowfish) using the postgresql crypto-functions.</p> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/users/hoti" typeof="schema:Person" property="schema:name" datatype="">Hoti</span></span> <span class="field field--name-created field--type-created field--label-hidden">Jun 18 2015</span> <div class="field field--name-field-tagies field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class='field__items'> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/database" hreflang="en">Database</a></div> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/postgresql" hreflang="en">Postgresql</a></div> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/sysadmin" hreflang="en">Sysadmin</a></div> </div> </div> <section class="field field--name-comment-node-blog field--type-comment field--label-hidden comment-wrapper"> </section> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class='title comment-form__title'> <i class="fa fa-comments-o"></i> Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=93&amp;2=comment&amp;3=comment" token="m8WJdp0OwOP8hHc_vuOeCCceEyWgsmUzNOJuoeOf-H4"></drupal-render-placeholder> </section> Thu, 18 Jun 2015 21:06:33 +0000 Hoti 93 at https://tech.feedyourhead.at postgresql: cheat-sheet https://tech.feedyourhead.at/content/postgresql-cheat-sheet <span class="field field--name-title field--type-string field--label-hidden">postgresql: cheat-sheet</span> <div class="clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item"><p><img alt="" src="http://upload.wikimedia.org/wikipedia/de/thumb/4/4b/Postgresql.svg/200px-Postgresql.svg.png" style="height:206px; width:200px" /></p> <h2>Show Tables</h2> <pre> <code> SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'; </code></pre> <p>Or simply type \d</p> <h2>Show Databases</h2> <pre> <code> SELECT datname FROM pg_database; </code></pre> <p>Or simply type \l</p> <h2>Show columns</h2> <pre> <code> SELECT column_name FROM information_schema.columns WHERE table_name ='table'; </code></pre> <p>Or simply type \d table</p> <h2>Describe Table</h2> <pre> <code> SELECT column_name FROM information_schema.columns WHERE table_name ='table'; </code></pre> <p>Or simply type \d+ table</p> <h2>Backup a database (command line)</h2> <pre> <code> pg_dump dbName &gt; dbName.sql </code></pre> <h2>Backup all databases (command line)</h2> <pre> <code> pg_dumpall &gt; pgbackup.sql </code></pre> <h2>Create user</h2> <pre> <code> CREATE USER tom WITH PASSWORD 'myPassword'; </code></pre> <h2>Create database</h2> <pre> <code> CREATE DATABASE mydatabase; </code></pre> <h2>Grant all privileges on database</h2> <pre> <code> GRANT ALL PRIVILEGES ON DATABASE mydatabase to tom; </code></pre> </div> <span class="field field--name-uid field--type-entity-reference field--label-hidden"><span lang="" about="/users/hoti" typeof="schema:Person" property="schema:name" datatype="">Hoti</span></span> <span class="field field--name-created field--type-created field--label-hidden">Apr 30 2015</span> <div class="field field--name-field-tagies field--type-entity-reference field--label-above"> <div class="field__label">Tags</div> <div class='field__items'> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/sysadmin" hreflang="en">Sysadmin</a></div> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/database" hreflang="en">Database</a></div> <div class="field__item"><i class="fa fa-tags"></i> <a href="/tags/postgresql" hreflang="en">Postgresql</a></div> </div> </div> <section class="field field--name-comment-node-blog field--type-comment field--label-hidden comment-wrapper"> </section> <section class="field field--name-comment field--type-comment field--label-above comment-wrapper"> <h2 class='title comment-form__title'> <i class="fa fa-comments-o"></i> Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=79&amp;2=comment&amp;3=comment" token="y6aSz7f79W3ef0kVx6rk1cOYe5C7yd5fSswe1ijNpr0"></drupal-render-placeholder> </section> Thu, 30 Apr 2015 18:09:14 +0000 Hoti 79 at https://tech.feedyourhead.at