Multiple database directories with postgresql I installed postgresql from source on Ubuntu 14.0.4. It is located in /usr/local/pgsql. At first, I created a data directory at /usr/local/pgsql/data and then ran

initdb -D /usr/local/pgsql/data


I noticed inside this directory was a postgresql.conf.

Later I decided I wanted to put the data directory in the /var directory, since it is variable data. So I created a directory at /var/local/postgresql/9.4.2/main (I guess I should have named it data, but when using the repositories it uses main and I decided to follow that). Then I ran initdb again but this time specifying a new data directory:

initdb -D /var/local/postgresql/9.4.2/main


And pretty much it created the same data files as it did before, including the postgresql.conf. This time I opened up the postgresql.conf at /var/local/postgresql/9.4.2/main and gave it a default data directory:

data_directory = '/var/local/postgresql/9.4.2/main'


Now, at this point I wanted to start the postmaster daemon but I want the daemon to read the data directory from /var/local/postgresql/9.4.2/main rather than me specifying it as a flag. Unfortunately, it doesn't work:

$ pg_ctl start
pg_ctl: no database directory specified and environment variable PGDATA unset
Try "pg_ctl --help" for more information.


How can I get this to work?
I got this working without specifying database directory, but you do need to specify the configuration file, so that the pg_ctl binary will know which data directory to use. So for example:

pg_ctl -D /etc/postgresql/9.4.2/main start