This short tutorial will show you how to start, stop, and restart PostgreSQL servers on your Linux system.
PostgreSQL is a flexible database system that allows you to run multiple versions of the server software on the same host. This can get a bit confusing, but thankfully PostgreSQL includes all of the tools you need to make sense of things and control each server individually.
How to Start PostgreSQL Service/Server
To start the default or primary PostgreSQL server on your system, simply run the following command:
systemctl start postgresql
The above command should work on all major Linux distributions.
How to Stop PostgreSQL Service/Server
To stop the default or primary PostgreSQL server on your system, simply run the following command:
sudo systemctl stop postgresql
The above command should work on all major Linux distributions.
How to Restart PostgreSQL Service/Server
To restart the default or primary PostgreSQL server on your system, simply run the following command:
sudo systemctl restart postgresql
The above command should work on all major Linux distributions.
Starting/Stopping Individual Clusters
If you have several running PostgreSQL clusters on your system (for example, two versions as the result of an upgrade), you may want to control the state of each cluster individually.
First, list the installed clusters by running the pg_lsclusters command:
sudo pg_lsclusters
The output should look something like this:
Ver Cluster Port Status Owner Data directory Log file 9.1 main 5433 online postgres /var/lib/postgresql/9.1/main /var/log/postgresql/postgresql-9.1-main.log 11 main 5432 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
The state of each cluster can then be controlled using the below commands:
sudo pg_ctlcluster VERSIONNUMBER stop sudo pg_ctlcluster VERSIONNUMBER start
How to Start the PostgreSQL Service/Server Automatically on Boot
You can also enable or disable the automatic starting of the PostgreSQL server when your computer boots by running one of the following commands:
systemctl enable postgresql systemctl disable postgresql
If you’re new to PostgreSQL, check out our ever-evolving guide for developers here.