On each platform there is a specific command to start the server.
If you are using a modern Linux distribution, you are probably using systemd. In this case, PostgreSQL can be started with the following command:
systemctl start SERVICEUNIT
This must be issued with OS superuser privileges, after replacing SERVICEUNIT with the appropriate systemd service unit name.
There are a couple of things to keep in mind:
- This will work only if the user executing the command has been previously granted appropriate sudo privileges by the system administrator
- If the command is executed from a superuser account, then the sudo keyword is unnecessary, although not harmful
As mentioned previously, the service unit name depends on what distribution you are using. Precisely:
- On Ubuntu and Debian there is a service unit called:
postgresql@RELEASE-CLUSTERNAME
- For each database server instance, there is another service unit called just postgresql, that can be used to manage all the database servers at once. So you can issue, for example:
sudo systemctl start postgresql
- To start all the available instances, and to start only the default version 10 instance:
sudo systemctl start postgresql@10-main
- Default Red Hat/Fedora packages call the service unit simply postgresql, so the syntax is as follows:
sudo systemctl start postgresql
- Red Hat/Fedora packages from the PostgreSQL Yum repository create a service unit called postgresql--RELEASE, so we can start version 10 as follows:
sudo systemctl start postgresql-10
As noted previously, systemctl is part of systemd, which is only available on Linux and is normally used by most of the recent distributions.
The following commands can be used where systemd is not available:
- On Debian and Ubuntu releases, you must invoke the PostgreSQL-specific utility pg_ctlcluster as follows:
pg_ctlcluster 10 main start
- For Red Hat/Fedora, you can use this command:
service postgresql start
- For Windows, the command is as follows:
net start postgres
- For Red Hat/Fedora, you can also use the following command:
pg_ctl -D $PGDATA start
where PGDATA is set to the data directory path.
In fact, this command works on most distributions, including macOS, Solaris, and FreeBSD, although:
- It is recommended to use, whenever possible, the distribution-specific syntax described previously.
- You may have to specify the full path to the pg_ctl executable if it's not in your path already. This is normally the case with multi-version directory schemes such as Debian/Ubuntu, where distribution-specific scripts pick the appropriate executable for your version.