147

How can I tell if my Postgresql server is running or not?

I'm getting this message:

[~/dev/working/sw] sudo bundle exec rake db:migrate 
rake aborted!
could not connect to server: Connection refused
    Is the server running on host "localhost" and accepting
    TCP/IP connections on port 5432?

Update:

> which postgres
/usr/local/bin/postgres
> pg_ctl -D /usr/local/bin/postgres -l /usr/local/bin/postgres/server.log start
pg_ctl: could not open PID file "/usr/local/bin/postgres/postmaster.pid": Not a directory

Update 2:

>pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
sh: /usr/local/var/postgres/server.log: No such file or directory
1
  • 2
    You should also look out for multiple versions of postgres on Mac OS/X. If you've installed postgres via homebrew then you can get the above error when your path is incorrectly setup - I just accidentally fudged my path and everything started using the system install of postgres which didn't work very well until I adjusted the path to use the brew install
    – Jamie Cook
    Commented Feb 16, 2013 at 8:05

7 Answers 7

141

The simplest way to to check running processes:

ps auxwww | grep postgres

And look for a command that looks something like this (your version may not be 8.3):

/Library/PostgreSQL/8.3/bin/postgres -D /Library/PostgreSQL/8.3/data

To start the server, execute something like this:

/Library/PostgreSQL/8.3/bin/pg_ctl start -D /Library/PostgreSQL/8.3/data -l postgres.log
4
  • ok and if i don't find anything like that, how do I start the server?
    – Ramy
    Commented Nov 2, 2011 at 3:29
  • 4
    pgrep postgres also works. Or ps auxwww | grep [p]ostgres to prevent the grep from getting picked up. Commented Nov 28, 2016 at 18:59
  • 2
    Boo, hiss re: the ps | grep malpractice suggestion. pgrep is acceptable, barely. pg_ctl status is great. ps auxwww | grep postgres is going to match grep postgres, and less /var/log/postgres/whatever. Commented Mar 11, 2019 at 15:30
  • it should be noted that the database cluster directory /Library/PostgreSQL/8.3/data is arbitrarily created by the user via the initdb command. here is the 8.3 doc
    – user3456074
    Commented Aug 26, 2019 at 22:06
81

You can run the following command to determine if postgress is running:

$ pg_ctl status

You'll also want to set the PGDATA environment variable.

Here's what I have in my ~/.bashrc file for postgres:

export PGDATA='/usr/local/var/postgres'
export PGHOST=localhost
alias start-pg='pg_ctl -l $PGDATA/server.log start'
alias stop-pg='pg_ctl stop -m fast'
alias show-pg-status='pg_ctl status'
alias restart-pg='pg_ctl reload'

To get them to take effect, remember to source it like so:

$ . ~/.bashrc

Now, try it and you should get something like this:

$ show-pg-status
pg_ctl: server is running (PID: 11030)
/usr/local/Cellar/postgresql/9.2.4/bin/postgres
1
  • Given your error message, I bet SamGoody's suggestion to run your initdb command will fix your "Connection refused" problem. Once it's fixed, try my suggestions to get your postgres db server status.
    – l3x
    Commented Sep 2, 2013 at 21:59
40

As of PostgreSQL 9.3, you can use the command pg_isready to determine the connection status of a PostgreSQL server.

From the docs:

pg_isready returns 0 to the shell if the server is accepting connections normally, 1 if the server is rejecting connections (for example during startup), 2 if there was no response to the connection attempt, and 3 if no attempt was made (for example due to invalid parameters).

1
  • How do you check the same for psql before 9.3?
    – BRBdot
    Commented Aug 6, 2018 at 22:23
33

You probably did not init postgres.

If you installed using HomeBrew, the init must be run before anything else becomes usable.

To see the instructions, run brew info postgres

# Create/Upgrade a Database
If this is your first install, create a database with:
     initdb /usr/local/var/postgres -E utf8

To have launchd start postgresql at login:
   ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents 
Then to load postgresql now:     
   launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist 
Or, if you don't want/need launchctl, you can just run:
    pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Once you have run that, it should say something like:

Success. You can now start the database server using:

postgres -D /usr/local/var/postgres or
pg_ctl -D /usr/local/var/postgres -l logfile start

If you are still having issues, check your firewall. If you use a good one like HandsOff! and it was configured to block traffic, then your page will not see the database.

1
  • 2
    brew info postgresql these days
    – tgf
    Commented Jul 13, 2023 at 4:37
15

You can use brew to start/stop pgsql. I've following short cuts in my ~/.bashrc file

alias start-pg='brew services start postgresql'
alias stop-pg='brew services stop postgresql'
alias restart-pg='brew services restart postgresql'
4
  • 13
    Question about How to check status not to start/stop pgsql!
    – moskito-x
    Commented Oct 15, 2020 at 18:12
  • 2
    you can try... brew services list | grep postgres
    – Raghuram
    Commented Oct 16, 2020 at 11:47
  • Love those aliases! Commented Jul 27, 2021 at 19:31
  • 1
    brew services info postgresql will provide the info
    – kartheek
    Commented Apr 6, 2022 at 4:28
13

It depends on where your postgresql server is installed. You use the pg_ctl to manually start the server like below.

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
3
  • @Ramy You substituted bin for var in the path. Commented Nov 2, 2011 at 3:51
  • hmmm...ok. i thought i needed to check for where my install was. please see new update.
    – Ramy
    Commented Nov 2, 2011 at 3:55
  • How did you install postgres? Did you run initdb after installing postgres?
    – Nvick
    Commented Nov 2, 2011 at 13:30
6

The pg_ctl status command suggested in other answers checks that the postmaster process exists and if so reports that it's running. That doesn't necessarily mean it is ready to accept connections or execute queries.

It is better to use another method like using psql to run a simple query and checking the exit code, e.g. psql -c 'SELECT 1', or use pg_isready to check the connection status.

1
  • 3
    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).
    – Kate
    Commented Sep 30, 2015 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.