Start, stop, configure PostgreSQL through Mac terminal
-
01-10-2020 - |
Question
I am having basic problems using PostgreSQL on my Mac OSX Yosemite, through the terminal. I tried google-ing but this is becoming increasingly difficult and info very scattered so I keep running into walls.
I did the following:
- installed PostgreSQL 9.5.2 via Homebrew
- modified my shell config file (.zshrc) to include PostgreSQL bin directory in $PATH variable
- I did the "initdb" routine and "told" Postgres that I want to store my databases in
/usr/local/cellar/postgresql/9.5.2/data
Here I tried starting postgreSQL:
$pg_ctl /usr/local/cellar/postgresql/9.5.2/data -l logfile start
first error:
pg_ctl: another server might be running; trying to start server anyway
/bin/sh: logfile: Permission denied
As far as I understand, I am trying to start the PostgreSQL server and direct its output and errors into a log file, located in a location for which my user has no write privileges.
How can I fix this?
Here I tried stopping the server and try again:
$pg_ctl -D /usr/local/cellar/postgresql/9.5.2/data stop
This worked ok. So,
- How can I use other location to start the server and enable log
- How do I check if the server is running if I need to stop it (if errors appear)?
Solution
The first error indcates the server is already running. Have you tried connecting with psql or run the following to establish that?
pg_ctl -D /usr/local/cellar/postgresql/9.5.2/data status
If you know it is down, then for the second error with the logfile on startup, either reissue the command with an absolute path to where you can write a file:
pg_ctl -D /usr/local/cellar/postgresql/9.5.2/data -l /tmp/logfile start
Or if that doesn't work for you, try starting without the -l switch:
pg_ctl -D /usr/local/cellar/postgresql/9.5.2/data start
Without the switch, you still get the PostgreSQL logs in the pg_log directory under the $PGDATA - in your case /usr/local/cellar/postgresql/9.5.2/data/pg_log
OTHER TIPS
To add on the previous answer - worth mentioning that in my case (Postgres 10/MacOS X High Sierra) the data storage location (the path at -D option) was different: /Library/PostgreSQL/10/data
So e.g. to stop the server I used:
sudo su - postgres
pg_ctl -D /Library/PostgreSQL/10/data stop
Or the same command using the postgres
user's home directory (which might be more universal for future versions):
sudo su - postgres
pg_ctl -D ~/data stop
Starting/checking status is similar (just use start
or status
instead of stop
)