1. Overview

PostgreSQL is a well-known database system released under an open-source license. Many major companies and organizations use it to store their data. Also, it’s supported by most operating systems.

In this tutorial, we’ll learn how to start the PostgreSQL server in Linux.

2. Starting the Server via the Service Manager

Most packaged versions of PostgreSQL install a background service to start and stop the server. Using the installed service is the recommended way to manage the server process.

Currently, the name of the service in the supported Linux systems is postgresql. This includes Ubuntu, Debian, Red Hat, Fedora, AlmaLinux, RockyLinux, and SUSE.

So, on a system built on systemd, we may use the systemctl command. In particular, let’s use systemctl status to check if the server is running:

$ systemctl status postgresql
○ postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; disabled; vendor preset: enabled)
     Active: inactive (dead)

Indeed, there’s a postgresql service that’s inactive and disabled. So, the server neither runs nor is started on system startup.

First, let’s start the server using systemctl:

$ sudo systemctl start postgresql
$ systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; disabled; vendor preset: enabled)
     Active: active (exited) since Thu 2024-02-01 16:03:43 EET; 3s ago
    Process: 51216 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 51216 (code=exited, status=0/SUCCESS)
        CPU: 1ms

As expected, the service started.

Next, let’s enable the service so that it starts on system boot:

$ sudo systemctl enable postgresql
Synchronizing state of postgresql.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable postgresql

As a result, the system should start the service during startup.

3. Using the postgres Program

The PostgreSQL server’s executable that starts the server in the foreground is called postgres. This is what a service configuration calls to actually run PostgreSQL.

Depending on the distribution, we may find it under the /usr/bin or /usr/lib/postgresql directories. In any case, we can consult the documentation of the PostgreSQL package we installed to find its location on our system. Another way is to use a command like dpkg -l that lists the installed files of a package.

3.1. Starting the Server Using postgres

The postgres program requires the path to the data directory or the PostgreSQL configuration file.

The program locates the data directory either through the -D option or through the PGDATA environment variable:

$ sudo -u postgres ./postgres -D /var/lib/pgsql/data
2024-01-24 14:28:14.098 EST [3815] LOG: redirecting log output to logging collector process
...

Indeed, the PostgreSQL started in the foreground. In this example, the path to the data directory is /var/lib/pgsql/data.

On the other hand, we can use the –config-file option to define the path to the configuration file:

$ sudo -u postgres ./postgres --config-file=postgresql.conf
2024-02-05 10:46:03.054 EET [68367] DEBUG:  postgres: PostmasterMain: initial environment dump:
...

As expected, the server started as in the previous example.

Notably, we started the server using the postgres user via sudo -u. Running the server with the postgres user instead of the current shell user account may prevent a potentially compromised server from corrupting the database files.

3.2. Running postgres as a Job

We can execute postgres as a job using the & operator:

$ sudo -i -u postgres
$ ./postgres --config-file=postgresql.conf >postgresql.log 2>&1 &

As expected, PostgreSQL started in the background. We started a new shell session as the postgres user, using sudo -i. As a result, the server is running using the postgres account.

Also, we performed two redirections. First, we redirected the standard error file descriptor to the standard output with 2>&1. Then, we redirected the logs to the postgresql.log file.

3.3. Setting Configuration Parameters via the Command Line

We can override the configuration values stored in postgresql.conf file via the -c option of the postgres program.

For instance, let’s change the value of the log_min_messages parameter. First, let’s find out its value in postgresql.conf file:

$ cat postgresql.conf | grep log_min_messages
log_min_messages = warning              # values in order of decreasing detail:

As we can see, the value of log_min_messages is warning.

Now, let’s change the value using the -c option:

$ ./postgres -c log_min_messages=debug5 --config-file=postgresql.conf >postgresql.log 2>&1 &

Indeed, we started the PostgreSQL server with the debug5 value set to the log_min_messages parameter.

To verify, let’s check that the server loaded the new value:

$ sudo -u postgres psql -d postgres -c 'show log_min_messages'
 log_min_messages
------------------
 debug5
(1 row)

As expected, log_min_messages is now debug5 instead of warning.

4. The pg_ctl Command

The pg_ctl program is a wrapper of the postgres command that provides a simpler syntax.

As with the postgres command, it’s best to run pg_ctl with the postgres user account. Let’s review some of the supported sub-commands.

4.1. start

The start sub-command starts a new PostgreSQL server instance in the background. The command tries to find the data directory through the -D option or the PGDATA environment variable. Furthermore, we may set a log file using the -l option:

$ sudo -u postgres pg_ctl start -D /var/lib/pgsql/data -l postgresql.log
 waiting for the server to start .... done
 server started

Indeed, pg_ctl started the server as expected.

4.2. status

We can use the status sub-command to check if the server is running:

$ sudo -u postgres pg_ctl status -D /var/lib/pgsql/data
pg_ctl: the server is running (PID: 6946)
/usr/bin/postgres "-D" "/var/lib/pgsql/data"

As expected, status reports that the server is running. We can also see the PID number of the server’s process.

4.3. stop

The stop sub-command shuts down the server. It supports three different modes for shutting down the server:

  1. smart: rejects new connections, waits for existing clients and online backups to end
  2. fast: rollbacks running transactions, disconnects clients, and stops online backups
  3. immediately: aborts all server processes, crash recovery runs in the next server startup

We define the stop mode with the -m option. If we don’t set the -m option, fast is selected by default.

Let’s stop the server using the smart mode:

$ sudo -u postgres pg_ctl stop -D /var/lib/pgsql/data -m smart
waiting for server to shut down.... done
server stopped

Indeed, the server is now stopped.

5. The pg_ctlcluster Command

The pg_ctlcluster command is a wrapper of the pg_ctl command, found in Ubuntu and other Debian-based systems. Like pg_ctl, it provides sub-commands like start, status, and stop.

Instead of the data directory, pg_ctlcluster requires a PostgreSQL cluster name and version.

We can list all PostgreSQL clusters with the pg_lsclusters command:

$ pg_lsclusters
Ver Cluster Port Status Owner    Data directory              Log file
14  main    5432 online postgres     ...                        ...

Here, we can see both the name and version of our cluster. In this example, the version is 14 while the cluster name is main.

Next, let’s start the server using pg_ctlcluster:

$ sudo -u postgres pg_ctlcluster 14 main start
Warning: the cluster will not be running as a systemd service. Consider using systemctl:
  sudo systemctl start postgresql@14-main

Indeed, we started the main cluster using the start sub-command.

6. Conclusion

In this article, we learned four methods to start the PostgreSQL server. The first is to use the systemctl service manager, which is often the recommended way to go. The rest of the methods use the postgres, the pg_ctl, and the pg_ctlcluster commands.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.