RE: Moving the data directory for systemd service startup

From: Michael Ireland <mireland(at)esri(dot)com>
To: Evan Rempel <erempel(at)uvic(dot)ca>, "pgsql-admin(at)lists(dot)postgresql(dot)org" <pgsql-admin(at)lists(dot)postgresql(dot)org>
Subject: RE: Moving the data directory for systemd service startup
Date: 2018-07-23 18:02:15
Message-ID: BYAPR05MB44726A719941BF9255E1A2DCAC560@BYAPR05MB4472.namprd05.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Hi,
Here is how I setup PGDATA in a non-default location on RHEL 7 for PostgreSQL 9.6, installed with postgres.org RPMs, leaving the /usr/lib/systemd/system/postgresql-9.6.service file unchanged:

[root(at)pgs-p01 ~]# mkdir /data/postgres/pgdata
[root(at)pgs-p01 ~]# chown -R postgres:postgres /data/postgres

[postgres(at)pgs-p01 ~]$ pg_ctl -D /data/postgres/pgdata initdb

[root(at)pgs-p01 ~]# vi /etc/systemd/system/postgresql-9.6.service
[root(at)pgs-p01 ~]# cat /etc/systemd/system/postgresql-9.6.service
.include /lib/systemd/system/postgresql-9.6.service

[Service]
Environment=PGDATA=/data/postgres/pgdata
[root(at)pgs-p01 ~]# systemctl daemon-reload
[root(at)pgs-p01 ~]# systemctl enable postgresql-9.6
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-9.6.service to /etc/systemd/system/postgresql-9.6.service.
[root(at)pgs-p01 ~]# systemctl start postgresql-9.6
[root(at)pgs-p01 ~]# systemctl status postgresql-9.6
â postgresql-9.6.service - PostgreSQL 9.6 database server
Loaded: loaded (/etc/systemd/system/postgresql-9.6.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2018-04-04 13:29:26 PDT; 12s ago
Docs: https://urldefense.proofpoint.com/v2/url?u=https-3A__www.postgresql.org_docs_9.6_static_&d=DwIGaQ&c=n6-cguzQvX_tUIrZOS_4Og&r=qmukiPJHYhgc3gzUy0nJGagHTl7faSE78XBWd5BdKPA&m=ir5ygAgBNo2KraRXN9JazrYzNEpYQ_9GBIkoZbKhkbM&s=ESUfZHAfPnY5S0O672sO_m1Jmjzaj5rxbeD8K5_Bpr4&e=
Process: 23865 ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 23871 (postmaster)
CGroup: /system.slice/postgresql-9.6.service
ââ23871 /usr/pgsql-9.6/bin/postmaster -D /data/postgres/pgdata
ââ23873 postgres: logger process
ââ23875 postgres: checkpointer process
ââ23876 postgres: writer process
ââ23877 postgres: wal writer process
ââ23878 postgres: autovacuum launcher process
ââ23879 postgres: stats collector process
Apr 04 13:29:26 pgs-p01.xxx.com systemd[1]: Starting PostgreSQL 9.6 database server...
Apr 04 13:29:26 pgs-p01.xxx.com postmaster[23871]: < 2018-04-04 13:29:26.297 PDT > LOG: redirecting log output ...cess
Apr 04 13:29:26 pgs-p01.xxx.com postmaster[23871]: < 2018-04-04 13:29:26.297 PDT > HINT: Future log output will...og".
Apr 04 13:29:26 pgs-p01.xxx.com systemd[1]: Started PostgreSQL 9.6 database server.
[root(at)pgs-p01 ~]#

[postgres(at)pgs-p01 ~]$ grep PGDATA= /usr/lib/systemd/system/postgresql-9.6.service
Environment=PGDATA=/var/lib/pgsql/9.6/data/
[postgres(at)pgs-p01 ~]$

-----Original Message-----
From: Evan Rempel [mailto:erempel(at)uvic(dot)ca]
Sent: Monday, July 23, 2018 10:20 AM
To: pgsql-admin(at)lists(dot)postgresql(dot)org
Subject: Moving the data directory for systemd service startup

I am using the official RPMs for RHEL-7 and am having difficulty in using a different database directory than the default of

/var/lib/pgsql/9.6/data/
or
/var/lib/pgsql/10/data/

It is easy to modify the service file

/usr/lib/systemd/system/postgresql-9.6.service

with the line

Environment=PGDATA=/var/lib/pgsql/9.6/data/

to point to the location I want, but an RPM update will replace this file and break database startup.

I was hoping that there would be a line

EnvironmentFile=-/etc/sysconfig/pgsql/postgresql-9.6

or

EnvironmentFile=-/etc/sysconfig/pgsql/postgresql-10

to allow for customization of the environment variables, similar to the init.d startup, but I don't see one.

Am I missing something, or is the configuration of the systemd unit file an oversight?

Any information would be helpful.

Evan.

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Mariel Cherkassky 2018-07-23 18:03:32 pg_authid has duplicated rows
Previous Message Johannes Truschnigg 2018-07-23 17:40:49 Re: Moving the data directory for systemd service startup