pg_ctl: no database directory specified and environment variable PGDATA unset

If you tried to run something like:

1
$ pg_ctl restart

and got the following error:

1
“ pg_ctl: no database directory specified and environment variable PGDATA unset

Then you probably need to specify the data directory (or database cluster) to your PG server.

If you don’t already has a directory for this:

1
$ mkdir ~/.postgres

Then initialize your data directory using the command initdb and the created directory:

1
$ initdb ~/.postgres

Hopefully everything will work now and you can initialize/restart your server again:

1
$ pg_ctl -D ~/.postgres start

avatar

Tailor Fontela

Software Developer. Full-time apprenticeship.