Postgresql – Start, stop, configure PostgreSQL through Mac terminal

mac os xpostgresqlpostgresql-9.5

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:

  1. installed PostgreSQL 9.5.2 via Homebrew
  2. modified my shell config file (.zshrc) to include PostgreSQL bin directory in $PATH variable
  3. 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)?

Best Answer

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