SmoothLinux Blog

Ubuntu Changing PostgreSQL Data Location

28 March, 2016 | postgresql

This will be a quick run down on how to change the data location of PostgreSQL. If you just change the data location without running these steps you will run into error trying to start PostgreSQL. PostgreSQL will not auto recreate the data files it needs to start. These steps were put together for version 9.3 of PostgreSQL.

Step 1) Stopping PostgreSQL Server (root user or user with sudo access)

sudo service postgresql stop

Step 2) Edit PostgreSQL's configuration file to point to another data location. The default for version 9.3 is /var/lib/postgresql/9.3/main

Edit File: /etc/postgresql/9.3/main/postgresql.conf

Old Location: 
data_directory = '/var/lib/postgresql/9.3/main'

New location:
data_directory = '/newlocation/postgresql/9.3/main'

Step 3) Sudo into postgres user

sudo su - postgres 

Step 4) Creating PostgreSQL default data files (Make sure directory newlocation is owned by user and group postgres )

/usr/lib/postgresql/9.3/bin/initdb -D /newlocation/postgresql/9.3/main

Step 5) Exit the postgres user account

exit

Step 6) Starting PostgreSQL Server (root user or user with sudo access)

sudo service postgresql start

Comments

comments powered by Disqus