Postgresql Change Data Directory in the service

1. Create the Override Drop-In

  1. Open the drop-in editor for the service:
    sudo systemctl edit postgresql-16.service
  2. 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
  3. 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.

Comments

Popular posts from this blog

Error when Installing Some Postgresql Packages (Perl IPC-Run)

Oracle Database Upgrade With OPatch Tool (RHEL/Centos/OEL)

POSTGRESQL UPGRADE WITH PG_UPGRADE UTILITY IN RHEL/CENTOS/OEL