1

I want to change the postgres' data directory to /home/postgres/data editing th postgresql.service instead of change the file /etc/postgresql/10/main/postgresql.conf.

So, I edited the service with

sudo systemctl edit postgresql.service

And added this lines:

[Service]
Environment=PGDATA=/home/postgres/data

And restart de service with sudo servicetl restart postgresql.service.

But the data directory don't changed.

Also edit [email protected] and reboot my laptop, but the data directory didn't change.

How can I change the data directory for postgres using systemctl services or units?

3
  • Does the output of ps aux |grep postgresql show your changed directory after the -D ? Did you actually move any data there?
    – ubfan1
    Oct 29, 2018 at 20:42
  • Here: postgres 9779 0.5 0.1 326888 27056 ? S 15:25 0:00 /usr/lib/postgresql/10/bin/postgres -D /var/lib/postgresql/10/main -c config_file=/etc/postgresql/10/main/postgresql.conf . Actually if I change the directory in postgres.conf the server runs correctly, in /home/postgres/, and the directory is owned by user and group. I mean, it seems the PGDATA variable is not used by postgres.
    – toledano
    Oct 29, 2018 at 21:26
  • Well, the old init.d type startup for postgresql explicitly warned you that env variables don't work: # Setting environment variables for the postmaster here does not work; please # set them in /etc/postgresql/<version>/<cluster>/environment instead.
    – ubfan1
    Oct 29, 2018 at 21:47

1 Answer 1

1

The data directory must be set in postgresql.conf files.

Why?

The way that PostgreSQL is packaged on Ubuntu, the postgresql.service is an umbrella service that can drive several instances of PostgreSQL running simultaneously (for instance you may have 10/main and 10/main2 or 9.6/main) and they cannot share the same $PGDATA, so defining it globally at the level of the umbrella service cannot work.

Also, defining it locally via service units like [email protected] would cause a problem to commands that manage PG instances like pg_lsclusters or pg_ctlcluster, as it's not clear how they would find what is the data directory of these instances. Currently these commands parse the configuration files. They do it for the data directory but also for other bits of data such as the port number or the Unix domain socket directories. This part is provided by the postgresql-common package which is pretty much independant from systemd (and predates its adoption).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .