{"id":515,"date":"2014-04-22T08:29:28","date_gmt":"2014-04-22T07:29:28","guid":{"rendered":"http:\/\/it.tuxie.eu\/?p=515"},"modified":"2014-04-22T08:38:58","modified_gmt":"2014-04-22T07:38:58","slug":"change-data-directory-postgresql","status":"publish","type":"post","link":"http:\/\/it.tuxie.eu\/?p=515","title":{"rendered":"Change data directory &#8211; PostgreSQL"},"content":{"rendered":"<p>Useful and common task, should be well documented. Let\u00b4s try it! Debian 7.4 x64, PostgreSQL 9.3.2<\/p>\n<p><!--more--><\/p>\n<p>I just checked via psql where is current data directory stored. Default path =\u00a0\/var\/lib\/postgresql\/9.3\/main . As a root i will create new directory, \/PGDATA and assign ownership into postgres user:<\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">root@deb74:\/var\/lib\/postgresql\/9.3\/main# <span style=\"color: #ff0000;\"><strong>mkdir -p \/PGDATA<\/strong><\/span><\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">root@deb74:\/var\/lib\/postgresql\/9.3\/main# <span style=\"color: #ff0000;\"><strong>chown -R postgres:postgres \/PGDATA<\/strong><\/span><\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">root@deb74:\/var\/lib\/postgresql\/9.3\/main# <span style=\"color: #ff0000;\"><strong>service postgresql stop<\/strong><\/span><\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">[ ok ] Stopping PostgreSQL 9.3 database server: main.<\/span><\/p>\n<p>Last step = pure paranoia level and untrustful &#8222;restart&#8220; service parameter \ud83d\ude42<\/p>\n<p>As postgres user I will re-initialize database:<\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">postgres@deb74:\/usr\/lib\/postgresql\/9.3\/bin$ <span style=\"color: #ff0000;\"><strong>\/usr\/lib\/postgresql\/9.3\/bin\/initdb -D \/PGDATA<\/strong><\/span><\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">The files belonging to this database system will be owned by user &#8222;postgres&#8220;.<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">This user must also own the server process.<\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">The database cluster will be initialized with locale &#8222;en_US.UTF-8&#8220;.<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">The default database encoding has accordingly been set to &#8222;UTF8&#8220;.<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">The default text search configuration will be set to &#8222;english&#8220;.<\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">Data page checksums are disabled.<\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">fixing permissions on existing directory \/PGDATA &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">creating subdirectories &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">selecting default max_connections &#8230; 100<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">selecting default shared_buffers &#8230; 128MB<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">creating configuration files &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">creating template1 database in \/PGDATA\/base\/1 &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">initializing pg_authid &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">initializing dependencies &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">creating system views &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">loading system objects&#8216; descriptions &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">creating collations &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">creating conversions &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">creating dictionaries &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">setting privileges on built-in objects &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">creating information schema &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">loading PL\/pgSQL server-side language &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">vacuuming database template1 &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">copying template1 to template0 &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">copying template1 to postgres &#8230; ok<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">syncing data to disk &#8230; ok<\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">WARNING: enabling &#8222;trust&#8220; authentication for local connections<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">You can change this by editing pg_hba.conf or using the option -A, or<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">&#8211;auth-local and &#8211;auth-host, the next time you run initdb.<\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">Success. You can now start the database server using:<\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">\/usr\/lib\/postgresql\/9.3\/bin\/postgres -D \/PGDATA<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">or<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\"> \/usr\/lib\/postgresql\/9.3\/bin\/pg_ctl -D \/PGDATA -l logfile start<\/span><\/p>\n<p>Change in configuration must be written into main postgresql conf file:<\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">postgres@deb74:~$ <span style=\"color: #ff0000;\"><strong>cd \/etc\/postgresql\/9.3\/main\/<\/strong><\/span><\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">postgres@deb74:\/etc\/postgresql\/9.3\/main$ <span style=\"color: #ff0000;\"><strong>nano postgresql.conf<\/strong><\/span><\/span><\/p>\n<p>There you have to change data_directory into <span style=\"color: #ff0000;\"><strong><code>'\/PGDATA'<\/code><\/strong><\/span>, save file.<\/p>\n<p>Now I need just to start postgresql service and double-check if everything is changed well:<\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">root@deb74:~# <span style=\"color: #ff0000;\"><strong>service postgresql start<\/strong><\/span><\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">[ ok ] Starting PostgreSQL 9.3 database server: main.<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">root@deb74:~# <span style=\"color: #ff0000;\"><strong>su postgres<\/strong><\/span><\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">postgres@deb74:\/root$ <span style=\"color: #ff0000;\"><strong>psql<\/strong><\/span><\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">could not change directory to &#8222;\/root&#8220;: Permission denied<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">psql (9.3.2)<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">Type &#8222;help&#8220; for help.<\/span><\/p>\n<p><span style=\"font-family: 'courier new', courier; font-size: 12px;\">postgres=# <span style=\"color: #ff0000;\"><strong>show data_directory;<\/strong><\/span><\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">data_directory <\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">&#8212;&#8212;&#8212;&#8212;&#8212;-<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\"> \/PGDATA<\/span><br \/>\n<span style=\"font-family: 'courier new', courier; font-size: 12px;\">(1 row)<\/span><\/p>\n<p>-a-<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Useful and common task, should be well documented. Let\u00b4s try it! Debian 7.4 x64, PostgreSQL 9.3.2<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,15],"tags":[],"_links":{"self":[{"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=\/wp\/v2\/posts\/515"}],"collection":[{"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=515"}],"version-history":[{"count":0,"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=\/wp\/v2\/posts\/515\/revisions"}],"wp:attachment":[{"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/it.tuxie.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}