Start and Stop PostgreSQL Service (Using pg_ctl and Related Commands)

What is pg_ctl?

pg_ctl is the control tool for the Postgres SQL server, which can be used to start, stop, restart and monitor PostgreSQL services.

Start PostgreSQL Service

Using pg_ctl start command

# Run the following command to start the PostgreSQL service
sudo pg_ctl start -D /var/lib/postgresql/13/main

This will start the PostgresSQL server in the specified data directory.

If you are using systemd

If your system uses systemd, then you can use the following command to start PostgresSQL:

# Run the following command to start PostgreSQL service
systemctl start postgresql@main.service

Stop PostgreSQL Service

Using pg_ctl stop command

# Run the following command to stop PostgreSQL service
sudo pg_ctl stop -D /var/lib/postgresql/13/main

This will stop PostgresSQL server in the specified data directory.

If you are using systemd

If your system uses systemd, then you can use the following command to stop PostgresSQL:

# Run the following command to stop PostgreSQL service
systemctl stop postgresql@main.service

Restart PostgreSQL Service

Using pg_ctl restart command

# Run the following command to restart PostgreSQL service
sudo pg_ctl restart -D /var/lib/postgresql/13/main

This will start and then stop PostgresSQL server in the specified data directory.

If you are using systemd

If your system uses systemd, then you can use the following command to restart PostgresSQL:

# Run the following command to restart PostgreSQL service
systemctl restart postgresql@main.service

Check PostgreSQL Service Status

Using pg_ctl status command

# Run the following command to check the status of PostgreSQL service
sudo pg_ctl status -D /var/lib/postgresql/13/main

This will display the current state of PostgresSQL server.

If you are using systemd

If your system uses systemd, then you can use the following command to check the status of PostgresSQL service:

# Run the following command to check the status of PostgreSQL service
systemctl status postgresql@main.service

This will display the current state of PostgresSQL server.