1. Create the Override Drop-In
- Open the drop-in editor for the service:
sudo systemctl edit postgresql-16.service
- In the editor that appears, paste the following above the “### Lines below…” comment:
[Service]
Environment=PGDATA=/your/custom/pgdata/path
### Lines below this comment will be discarded
- Save and exit:
• Nano:
Ctrl+O→Enter, thenCtrl+X• Vim::wq→Enter
2. Reload systemd and Restart PostgreSQL
sudo systemctl daemon-reload
sudo systemctl restart postgresql-16
sudo systemctl enable postgresql-16
3. Manual File Creation (Alternative)
If the editor method fails, create the drop-in file directly:
sudo mkdir -p /etc/systemd/system/postgresql-16.service.d
sudo tee /etc/systemd/system/postgresql-16.service.d/override.conf <<EOF
[Service]
Environment=PGDATA=/your/custom/pgdata/path
EOF
sudo systemctl daemon-reload
sudo systemctl restart postgresql-16
4. Verify Your New Data Directory
- Check service status:
systemctl status postgresql-16
- Confirm inside PostgreSQL:
sudo -u postgres psql -c "SHOW data_directory;"
- The output should match
/your/custom/pgdata/path.