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 directoryUpdate 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 7 Answers
The simplest way to to check running processes:
ps auxwww | grep postgresAnd 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/dataTo 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 You can run the following command to determine if postgress is running:
$ pg_ctl statusYou'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:
$ . ~/.bashrcNow, 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 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 startOnce 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.
As of PostgreSQL 9.3, you can use the command pg_isready to determine the connection status of a PostgreSQL server.
From the docs:
1pg_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).
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 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 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.