Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart PostgreSQL server on MacOS?

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

like image 931
daydreamer Avatar asked Nov 03 '11 05:11

daydreamer


People also ask

How do I start PostgreSQL on Mac terminal?

`psql` on Terminal To get to the PostgreSQL terminal, open your terminal, start the database services ( brew services start postgresql) , then run psql . Thank you!

How can I tell if postgres is running on my Mac?

psql -c "SELECT 1" -d {dbname} > /dev/null || postgres -D /usr/local/var/postgres >postgres. log 2>&1 & if you want to check and start postgres in one go (handy for automation scripts).


2 Answers

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)

like image 76
a_horse_with_no_name Avatar answered Oct 11 '22 07:10

a_horse_with_no_name


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
like image 25
pjammer Avatar answered Oct 11 '22 07:10

pjammer