How to move PostgreSQL datadir
The context
I run RHEL 7 and CentOS 7 .The sysadmin team has setup a virtual machine for me and they put a big "/data" partition.
The default PostgreSQL installation usually use "/var/lib/pgsql" for PostgreSQL location.
I want to change it to "/data/pgsql"
How to
Stop PostgreSQLsystemctl stop postgresql
Move "/var/lib/pgsql" and retain ownership & permissions
rsync -av /var/lib/pgsql /data/
Note: don't remove "/var/lib/pgsql" yet!
Configure "/data/pgsql/data/postgresql.conf" with the new location:
- #data_directory = 'ConfigDir' + data_directory = '/data/pgsql/data'
Configure systemd service file "/usr/lib/systemd/system/postgresql.service"
- Environment=PGDATA=/var/lib/pgsql/data + Environment=PGDATA=/data/pgsql/data
Start PostgreSQLand check it all:
# systemctl start postgresql # systemctl status postgresql # ps aux | grep pgsql # su - postgres $ psql postgres=# SHOW data_directory ; data_directory ------------------ /data/pgsql/data (1 row)
Warning message