Change data directory – PostgreSQL

Useful and common task, should be well documented. Let´s try it! Debian 7.4 x64, PostgreSQL 9.3.2

I just checked via psql where is current data directory stored. Default path = /var/lib/postgresql/9.3/main . As a root i will create new directory, /PGDATA and assign ownership into postgres user:

root@deb74:/var/lib/postgresql/9.3/main# mkdir -p /PGDATA
root@deb74:/var/lib/postgresql/9.3/main# chown -R postgres:postgres /PGDATA
root@deb74:/var/lib/postgresql/9.3/main# service postgresql stop
[ ok ] Stopping PostgreSQL 9.3 database server: main.

Last step = pure paranoia level and untrustful „restart“ service parameter 🙂

As postgres user I will re-initialize database:

postgres@deb74:/usr/lib/postgresql/9.3/bin$ /usr/lib/postgresql/9.3/bin/initdb -D /PGDATA
The files belonging to this database system will be owned by user „postgres“.
This user must also own the server process.

The database cluster will be initialized with locale „en_US.UTF-8“.
The default database encoding has accordingly been set to „UTF8“.
The default text search configuration will be set to „english“.

Data page checksums are disabled.

fixing permissions on existing directory /PGDATA … ok
creating subdirectories … ok
selecting default max_connections … 100
selecting default shared_buffers … 128MB
creating configuration files … ok
creating template1 database in /PGDATA/base/1 … ok
initializing pg_authid … ok
initializing dependencies … ok
creating system views … ok
loading system objects‘ descriptions … ok
creating collations … ok
creating conversions … ok
creating dictionaries … ok
setting privileges on built-in objects … ok
creating information schema … ok
loading PL/pgSQL server-side language … ok
vacuuming database template1 … ok
copying template1 to template0 … ok
copying template1 to postgres … ok
syncing data to disk … ok

WARNING: enabling „trust“ authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
–auth-local and –auth-host, the next time you run initdb.

Success. You can now start the database server using:

/usr/lib/postgresql/9.3/bin/postgres -D /PGDATA
/usr/lib/postgresql/9.3/bin/pg_ctl -D /PGDATA -l logfile start

Change in configuration must be written into main postgresql conf file:

postgres@deb74:~$ cd /etc/postgresql/9.3/main/

postgres@deb74:/etc/postgresql/9.3/main$ nano postgresql.conf

There you have to change data_directory into '/PGDATA', save file.

Now I need just to start postgresql service and double-check if everything is changed well:

root@deb74:~# service postgresql start
[ ok ] Starting PostgreSQL 9.3 database server: main.
root@deb74:~# su postgres
postgres@deb74:/root$ psql
could not change directory to „/root“: Permission denied
psql (9.3.2)
Type „help“ for help.

postgres=# show data_directory;
(1 row)


This entry was posted in Databases, PostgreSQL. Bookmark the permalink.

Napsat komentář