Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BlacksilverConsulting/cf4789bb2457ca4f56f65d4ebad5c9ab to your computer and use it in GitHub Desktop.
Save BlacksilverConsulting/cf4789bb2457ca4f56f65d4ebad5c9ab to your computer and use it in GitHub Desktop.
Configure PostgreSQL 14 to use an alternate PGDATA under systemd

This assumes the Ansible playbook pg14.yaml has already be run successfully. This means:

  • The PostgreSQL 14 Client and Server are already installed from the official PostgreSQL repos
  • The PostgreSQL 14 service in systemd is enabled
  • The PostgreSQL 14 service in systemd is configured to only start after autofs has started
  • A PostgreSQL 14 database cluster has been created at /var/lib/pgsql/14
  • The PostgreSQL 14 service in systemd is running

Configuring PostgreSQL 14 to use a different path for the database cluster will require undoing some of these things.

For this example, I am setting up a new database cluster at /mnt/data/lv_data/db14, and running all commands as root.

To start, shut down the running instance of PostgreSQL 14:

systemctl stop postgresql-14.service

Then change the service configuration. Note that you should not alter the actual service configuration file, instead use systemd's override.conf method for changing the configuration.

Add the following lines to the override.conf file:

[Service]
Environment=PGDATA=/mnt/data/lv_data/db14

An easy way to do this is systemctl edit postgresql-14.service, which will open the correct file in nano for you.

After making that change, you have to tell systemd to reload its configuration with systemctl daemon-reload.

Finally, set up the new cluster and start PostgreSQL 14:

# Create the cluster directory
mkdir /mnt/data/lv_data/db14
# Set ownership
chown postgres:postgres /mnt/data/lv_data/db14
# Initialize the cluster
postgresql-14-setup initdb
# Start the service
systemctl start postgresql-14.service

To confirm that PostgreSQL 14 is running the way you expect, use this command and look for the postmaster line: systemctl status postgresql-14.serice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment