Server management hacks

Using pg_ctl, ensure server starts/restarts correctly

Scenario: When starting/restarting PostgreSQL using pg_ctl, the output says "server starting" but doesn't indicate whether the server actually started, which results in situations like this:

elephant$ pg_ctl -D /path/to/data/ -l /tmp/pg.log start
server starting
$ psql -d postgres
psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Solution: Use pg_ctl's -w command line flag, which makes pg_ctl wait for completion of startup as well as shutdown (prior to PostgreSQL 10 the default is to wait for shutdown completion only).

$ pg_ctl -w -D /path/to/data/ -l /tmp/pg.log start
waiting for server to start........ stopped waiting
pg_ctl: could not start server
Examine the log output.
Use a postgresql.conf file outside of the data directory

Scenario: you need to keep postgresql.conf in a different location, e.g. to make server management easier.

Solution: pass "-c config_file=/path/to/postgresql.conf" to the PostgreSQL server at server start.

$ pg_ctl -D /path/to/data-directory -o "-c config_file=/path/to/postgresql.conf" -w start
waiting for server to start.... done
server started