0
0
PostgresqlHow-ToBeginner · 3 min read

How to Change Data Directory in PostgreSQL

To change the data directory in PostgreSQL, first stop the PostgreSQL service, then move the existing data folder to the new location. Next, update the data_directory setting in postgresql.conf to point to the new path, and finally restart the service.
📐

Syntax

The main step to change the data directory is to update the data_directory parameter in the postgresql.conf file. This parameter tells PostgreSQL where to find its data files.

Basic syntax in postgresql.conf:

data_directory = '/new/path/to/data'

Remember to stop the PostgreSQL server before moving data and changing this setting, and restart it after.

sql
data_directory = '/new/path/to/data'
💻

Example

This example shows how to move the PostgreSQL data directory from /var/lib/postgresql/14/main to /mnt/newdisk/pgdata on a Linux system.

Steps:

  • Stop PostgreSQL service
  • Copy data to new location
  • Update postgresql.conf
  • Restart PostgreSQL service
bash
# Stop PostgreSQL service
sudo systemctl stop postgresql

# Copy data directory to new location
sudo rsync -av /var/lib/postgresql/14/main /mnt/newdisk/pgdata

# Change ownership to postgres user
sudo chown -R postgres:postgres /mnt/newdisk/pgdata

# Edit postgresql.conf to update data_directory
# (Example using nano editor)
sudo nano /etc/postgresql/14/main/postgresql.conf
# Change or add:
data_directory = '/mnt/newdisk/pgdata'

# Start PostgreSQL service
sudo systemctl start postgresql

# Verify status
sudo systemctl status postgresql
Output
● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) Active: active (running) since ... Main PID: 1234 (postgres) Tasks: 7 (limit: 4915) Memory: 50.0M CGroup: /system.slice/postgresql.service ├─1234 /usr/lib/postgresql/14/bin/postgres -D /mnt/newdisk/pgdata └─1235 postgres: checkpointer process # No errors means data directory changed successfully.
⚠️

Common Pitfalls

  • Not stopping PostgreSQL before moving data: This can cause data corruption.
  • Incorrect permissions: The new data directory must be owned by the PostgreSQL user (usually postgres).
  • Wrong path in postgresql.conf: Make sure the path is absolute and correctly typed.
  • SELinux or AppArmor restrictions: Security modules may block access to the new directory.
bash
# Wrong way: Moving data without stopping service
sudo rsync -av /var/lib/postgresql/14/main /mnt/newdisk/pgdata

# Correct way:
sudo systemctl stop postgresql
sudo rsync -av /var/lib/postgresql/14/main /mnt/newdisk/pgdata
sudo chown -R postgres:postgres /mnt/newdisk/pgdata
# Update postgresql.conf
sudo systemctl start postgresql
📊

Quick Reference

StepCommand/ActionNotes
1Stop PostgreSQL servicesudo systemctl stop postgresql
2Copy data directorysudo rsync -av /old/path /new/path
3Set ownershipsudo chown -R postgres:postgres /new/path
4Edit postgresql.confSet data_directory = '/new/path'
5Start PostgreSQL servicesudo systemctl start postgresql
6Verify service statussudo systemctl status postgresql

Key Takeaways

Always stop PostgreSQL before moving the data directory to avoid corruption.
Update the data_directory setting in postgresql.conf to the new path.
Ensure the new data directory has correct ownership and permissions.
Check for security restrictions like SELinux or AppArmor that may block access.
Restart PostgreSQL after changes and verify it runs correctly.