PostgreSQL High Performance Cookbook
上QQ阅读APP看书,第一时间看更新

Starting the server manually

Normally, a PostgreSQL server will start automatically when the system boots up. If an automatic start is not enabled for a server, we would need to start the server manually. This may be required even for operational reasons.

Getting ready

Before we talk about how to start the database server, first we need to understand the difference between server and service. The term server refers to the database server and its processes, whereas the term service essentially indicates the operating system wrapper through which the server gets called.

How to do it...

On a majority of platforms, that is, Linux and Unix distributions, we can start the server using the pg_ctl command-line utility as shown here:

pg_ctl -D <location of data directory> start

Consider the following example:

pg_ctl -D /var/lib/pgsql/9.6/data start

The -D switch of the pg_ctl command indicates the data directory of the PostgreSQL server. In the preceding command, the data directory is defined at the location /var/lib/pgsql/9.6/data.

On Linux and Unix platforms such as Red Hat, the service can be started as mentioned here:

service <postgresql-version> start

For instance, to start the PostgreSQL server version 9.6 as a service, we can use the following command:

service postgresql-9.6 start

How it works...

Using the start mode of the pg_ctl command, the PostgreSQL server is started in the background and its background processes are initiated.

You can use the following command to check whether the PostgreSQL server background processes have started:

ps aux | grep "postgres" | grep -v "grep" postgres 1289 0.0 0.3 51676 7900 ? S 11:31 0:01 /usr/lib/postgresql/9.6/bin/postgres -D /var/lib/postgresql/9.6/main -c config_file=/etc/postgresql/9.6/main/postgresql.conf postgres 1303 0.0 0.0 21828 1148 ? Ss 11:31 0:00 postgres: logger process postgres 1305 0.0 0.0 51676 1568 ? Ss 11:31 0:02 postgres: writer process postgres 1306 0.0 0.0 51676 1324 ? Ss 11:31 0:02 postgres: wal writer process postgres 1307 0.0 0.1 52228 2452 ? Ss 11:31 0:00 postgres: autovacuum launcher process postgres 1308 0.0 0.0 22016 1432 ? Ss 11:31 0:00 postgres: stats collector process

When we execute the pg_ctl command, it internally performs various validations such as checking that the data directory that we mentioned is equal to the data_directory setting in postgresql.conf. If the data_directory parameter points to some other directory location, then PostgreSQL will instantiate to that directory location. After the validations have been completed, the pg_ctl binary internally invokes another process called postgres, which is the main process for the PostgreSQL cluster.