Leeladharan Achar - alias - leelu ~ blogging...hola mi amigos..'s Blog

Posted Oct. 10, 2019   93 views

Move PostgreSQL9.6 data directory to a different directory in CentOS 7

We wanted to move the PostgreSQL9.6 default data directory to home directory in centos7 due to space constraints of the partition we had in our root directory. So below are the steps to move default data directory of PostgreSQL to /home directory

Create a directory in /home for postgres data

Login as root and execute below commands

user$ su -l root
root$ systemctl stop postgresql-9.6
root$ mkdir /home/pguser
root$ rsync -av /var/lib/pgsql /home/pguser
root$ chown -R postgres:postgres pguser
root$ semanage fcontext --add --equal /var/lib/pgsql /home/pguser
root$ restorecon -rv /home/pguser

Override postgres systemd service for postgres data directory environment variable

root$ vi /etc/systemd/system/postgresql-9.6.service.d/override.conf

#once the file is opened add below content

[Service]
Environment=PGDATA=/home/pguser/pgsql/9.6/data

Restart Postgresql Server

root$ systemctl daemon-reload
root$ systemctl start postgresql-9.6