How can I start PostgreSQL server on Mac OS X?

Solution 1:

The Homebrew package manager includes launchctl plists to start automatically. For more information, run brew info postgres.

Start manually

pg_ctl -D /usr/local/var/postgres start

Stop manually

pg_ctl -D /usr/local/var/postgres stop

Start automatically

"To have launchd start postgresql now and restart at login:"

brew services start postgresql


What is the result of pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start?

What is the result of pg_ctl -D /usr/local/var/postgres status?

Are there any error messages in the server.log?

Make sure tcp localhost connections are enabled in pg_hba.conf:

# IPv4 local connections:
host    all    all    127.0.0.1/32    trust

Check the listen_addresses and port in postgresql.conf:

egrep 'listen|port' /usr/local/var/postgres/postgresql.conf

#listen_addresses = 'localhost'        # What IP address(es) to listen on;
#port = 5432                # (change requires restart)

Cleaning up

PostgreSQL was most likely installed via Homebrew, Fink, MacPorts or the EnterpriseDB installer.

Check the output of the following commands to determine which package manager it was installed with:

brew && brew list|grep postgres
fink && fink list|grep postgres
port && port installed|grep postgres

Solution 2:

If you want to manually start and stop PostgreSQL (installed via Homebrew), the easiest way is:

brew services start postgresql

and

brew services stop postgresql

If you have a specific version, make sure to suffix the version. For example:

brew services start postgresql@10

Solution 3:

I had almost the exact same issue, and you cited the initdb command as being the fix. This was also the solution for me, but I didn't see that anyone posted it here, so for those who are looking for it:

initdb /usr/local/var/postgres -E utf8

Solution 4:

If your computer was abruptly restarted


You may want to start PG server but it was not.

First, you have to delete the file /usr/local/var/postgres/postmaster.pid Then you can restart the service using one of the many other mentioned methods depending on your install.

You can verify this by looking at the logs of Postgres to see what might be going on: tail -f /usr/local/var/postgres/server.log

For specific version:-

 tail -f /usr/local/var/postgres@[VERSION_NUM]/server.log

Eg:

 tail -f /usr/local/var/postgres@11/server.log

Solution 5:

Another approach is using the lunchy gem (a wrapper for launchctl):

brew install postgresql
initdb /usr/local/var/postgres -E utf8
gem install lunchy

To start PostgreSQL:

lunchy start postgres

To stop PostgreSQL:

lunchy stop postgres

For further information, refer to: "How to Install PostgreSQL on a Mac With Homebrew and Lunchy"