alisvo.dev DBA
#Pgdata #PostgreSQL #Setup

Postgresql Change Data Directory in the service

Systemd ile yönetilen PostgreSQL servislerinde veri dizinini (data directory) güvenli bir şekilde farklı bir diske veya dizine taşıma.

AS
Ali Sağırvelioğulları
Senior Database Administrator
1 dk okuma

1. Create the Override Drop-In

  1. Open the drop-in editor for the service:
sudo systemctl edit postgresql-16.service
  1. 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
  1. Save and exit: • Nano: Ctrl+OEnter, then Ctrl+X • Vim: :wqEnter

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.