17

I tried the following and it failed

bash-3.2$ pg_ctl restart
pg_ctl: no database directory specified and environment variable PGDATA unset

I am using Macbook Pro and dont remember the directory where my data is stored and so can't set PGDATA either, is there a way to restart Postgres at all without losing data?

Thank you

MWiesner
  • 8,163
  • 11
  • 34
  • 68
daydreamer
  • 80,741
  • 175
  • 429
  • 691

3 Answers3

21

As you want to re-start Postgres I assume the server is already running.

Using the superuser account you can query the location of the data directory through SQL:

select name, setting
from pg_settings
where name = 'data_directory'

With that information you can supply the data directory to the pg_ctl command using the -D switch (see the manual for details)

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
18

If you used brew you could find the restart(start|stop) instructions by doing a brew info postgresql Some issues that happen stem from the fact I've seen the plist not load, on OS startup, for some reason. I always forgot what to do and this helped.

on my Postgres 9.2 brew recipe it had:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Kyle Decot
  • 19,788
  • 38
  • 138
  • 255
pjammer
  • 9,227
  • 4
  • 44
  • 56
  • 1
    This is exactly what I was looking for! Seems that there is very little-to-no brew related documentation on restarting postgres. – jgillman Mar 24 '14 at 21:22
15

One liner (with default postgresSql 9.6 install)

sudo su postgres -c "/Library/PostgreSQL/9.6/bin/pg_ctl restart -D /Library/PostgreSQL/9.6/data"

Full version

Get the pgdata folder (gave me /Library/PostgreSQL/9.6/data) (thanks @a_horse_with_no_name)

select name, setting from pg_settings where name = 'data_directory';

For me it returns '/Library/PostgreSQL/9.6/data'

Then

sudo su - postgres
./bin/pg_ctl restart -D /Library/PostgreSQL/9.6/data

Note: postgres' user home dir should be "/Library/PostgreSQL/9.6"

Jeremy Chone
  • 2,921
  • 1
  • 26
  • 28