- Check if PostgreSQL Server Is Running on macOS
grepCommand to Check the Status of PostgreSQL
pg_ctl statusCommand to Check the Status of PostgreSQL
pg_isreadyCommand to Check the Status of PostgreSQL
- Other Commands to Check the Status of PostgreSQL
This article is specially articulated to help you find the status of your PostgreSQL server on the MAC operating system. Several BASH commands have been mentioned in the article.
These can be used to check the status of PostgreSQL and whether it is running or not.
Note: The BASH commands mentioned throughout the article might not work for all operating systems; however, they are known to work well for macOS.
Numerous BASH commands have been mentioned as an alternative to each other; if one command does not work out, other commands can be executed to check the status of the PostgreSQL server.
Check if PostgreSQL Server Is Running on macOS
Numerous commands can be used, moving to the BASH commands to check the status of PostgreSQL. This article focuses on the commands that will help you determine whether PostgreSQL is running or not.
Before diving into the BASH commands, let’s look over the error messages you might receive when you try to access PostgreSQL.
Let’s have a look over the following messages and analyze if these seem familiar to you or not.
[~/dev/working/sw] sudo bundle exec rake db:migrate rake aborted! could not connect to the server: Connection refused Is the server running on the host "localhost" and accepting TCP/IP connections on port 5432?
Another error message that pops up when you use the
pg_ctl command is as follows:
> 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
These are two examples of error messages that you might have encountered while trying to run PostgreSQL on your system. These errors might result from an incorrect path setup during the initialization process.
When using the
initdb command to create a PostgreSQL database cluster, you need to provide the right path that points to the location of PostgreSQL. If there is any error in the path initialization, you need to correct it and then check whether PostgreSQL is working or not.
Several BASH command options have been provided below, so you can choose any of them to check the status of PostgreSQL. Let’s discuss each one of the commands in detail.
grep Command to Check the Status of PostgreSQL
The first BASH command that helps to check whether processes are running or not is as follows:
> ps auxwww | grep postgres
Or, you can try another version of the same command, which is:
> ps aux | grep postgres
The return message helps you identify if PostgreSQL is running or not. If you want to start the PostgreSQL server, you need to look for a command that is similar to the following.
/Library/PostgreSQL/12/bin/postgres -D /Library/PostgreSQL/12/data
In the command above,
12 represents the PostgreSQL version. If you have a different version of the PostgreSQL installed, you would see a different figure at the place of
To start the PostgreSQL server, use the following command.
/Library/PostgreSQL/12/bin/pg_ctl start-D /Library/PostgreSQL/12/data -l postgres.log
/Library/PostgreSQL/12/datais an arbitrary database cluster directory created by the user using
initdbin the initialization process.
These commands can help you identify if PostgreSQL is running or not. If PostgreSQL is not running, you can start it with the above command.
pg_ctl status Command to Check the Status of PostgreSQL
Another BASH command that checks the status of PostgreSQL is as follows:
> pg_ctl status
pg_ctl status command checks the existence of the postmaster process. The command reports that it is running if it can find the process.
Note: The running status does not indicate that the postmaster is ready to accept other connections or execute more queries.
PGDATA environment variable might also require some edits to check the status of PostgreSQL. Here is a depiction of the PostgreSQL
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'
Once you have edited the environment variable, it is essential to run the following command to source the file.
> . ~/.bashrc
After this, you can run the
show-pg-status command for PostgreSQL on BASH.
> show-pg-status pg_ctl: server isrunning (PID: 11030) /usr/local/Cellar/postgresql/9.2.4/bin/postgres
pg_ctl command can also be used to start the PostgreSQL server manually. It is essential to determine the path of PostgreSQL before running the command.
The path can be determined using the
which postgres command. It can then be used to start PostgreSQL manually.
> pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
pg_isready Command to Check the Status of PostgreSQL
Another interesting command you should look at is the
pg_isready command. This is used to check the connection status of the PostgreSQL server.
The execution of the
pg_isready command returns either of the following values:
0- depicts that the server accepts connections as usual.
1- is returned if the server is not accepting any connections.
2- it shows that no response was received following the connection attempt.
3- depicts that no connection attempt was made; this might be due to an error, for instance, using invalid parameters.
Other Commands to Check the Status of PostgreSQL
There are several other options for BASH commands that you can use to check the status of PostgreSQL.
Try using the
psql command or the
pgrep postgres command to determine if the PostgreSQL server is running on your MAC or not.
Different operating systems require different commands to perform the same function. The BASH commands mentioned in the article are known to perform well on macOS; however, you can also try these on other operating systems.