PostgreSQL

Restart Postgres

Service management is a prevalent task for system administrators and power users. For example, one of the common tasks of a database or system administrator is ensuring that the database servers are running smoothly.

This tutorial explores the various methods and techniques that you can use to restart the PostgreSQL server on different systems.

NOTE: It is good to keep in mind that the steps and techniques that are discussed in this tutorial may vary depending on the target system, installed version, and installation method.

Restarting PostgreSQL on Linux

On Linux, there are several methods that you can use to restart the PostgreSQL server. However, these methods may require sudo permissions.

Using the Systemd

To restart PostgreSQL using the systemd, open the terminal and run the following command as follows:

sudo systemctl restart postgresql

Using the Service Command

You can also use the service command to restart PostgreSQL on Linux. The command is as follows:

sudo service postgresql restart

Manual Restart

You can also perform a manual restart using the provided PostgreSQL binaries. The commands are as follows:

Open the terminal and locate the data directory for your PostgreSQL installation. The common locations are in /var/lib/postgresql/<version>/data or /var/lib/postgresql/<version>data.

Run the following command to stop the PostgreSQL server:

sudo su - postgres -c 'pg_ctl stop -D /path/to/data/directory'

Once the server is stopped, run the following command to start it back again.

sudo su - postgres -c 'pg_ctl start -D /path/to/data/directory'

This should bring back up the PostgreSQL server.

Restart PostgreSQL on MacOS

Similarly, the methods vary depending on the installation method in PostgreSQL.

Using Homebrew

If you installed PostgreSQL on macOS using Homebrew, you can use the Homebrew service manager as shown in the following command:

brew services restart postgresql

Restart PostgreSQL on Windows

On Windows, you can use two main methods to restart the PostgreSQL server.

Using the Command Prompt

Start by launching the command prompt as an administrator.

Next, navigate to the PostgreSQL bin directory. If you used the default install directory, head over to C:\Program Files\PostgreSQL\<version>\bin.

$ cd "C:\Program Files\PostgreSQL\15\bin\

Finally, run the following command to restart PostgreSQL:

pg_ctl.exe restart -D "C:\Program Files\PostgreSQL\<version>\data"

This should restart the PostgreSQL server using the pg_ctl utility. Remember to replace the version with your installed PostgreSQL version.

Using the Windows Services

You can also use the Windows Service administration tool. To launch the Services Manager, press the Windows Key + R and type “services.msc”.

Locate the PostgreSQL service from the services window.

Right-click on the service and select “Restart”.

Conclusion

We explored the various methods and techniques that you can use to restart the PostgreSQL server on Linux, Windows, and macOS.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list