Job for postgresql.service failed because the control process exited with error code

Introduction

Very often this error occurs because the database has not been initialized.

Error text

For example, you ran

sudo systemctl start postgresql

Job for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for details.

You check the status

systemctl status -l postgresql.service

● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Wed 2021-06-09 12:10:31 EEST; 1min 18s ago Process: 7223 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=1/FAILURE) Jun 09 12:10:31 localhost.localdomain systemd[1]: Starting PostgreSQL database server... Jun 09 12:10:31 localhost.localdomain systemd[1]: postgresql.service: control process exited, code=exited status=1 Jun 09 12:10:31 localhost.localdomain systemd[1]: Failed to start PostgreSQL database server. Jun 09 12:10:31 localhost.localdomain systemd[1]: Unit postgresql.service entered failed state. Jun 09 12:10:31 localhost.localdomain systemd[1]: postgresql.service failed.

Solution

Try to initialize the database (as the postgres user)

postgresql-setup initdb

Initializing database ... OK

Checking the result

sudo systemctl start postgresql
systemctl status postgresql.service

● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2021-06-09 12:22:00 EEST; 12s ago Process: 7741 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=0/SUCCESS) Process: 7734 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS) Main PID: 7746 (postgres) Tasks: 7 CGroup: /system.slice/postgresql.service ├─7746 /usr/bin/postgres -D /var/lib/pgsql/data -p 5432 ├─7749 postgres: logger process ├─7751 postgres: checkpointer process ├─7752 postgres: writer process ├─7753 postgres: wal writer process ├─7754 postgres: autovacuum launcher process └─7755 postgres: stats collector process Jun 09 12:21:59 localhost.localdomain systemd[1]: Starting PostgreSQL database server... Jun 09 12:22:00 localhost.localdomain systemd[1]: Started PostgreSQL database server.

Of course, the source of the error may be completely different.

The status often helps to figure it out. It is advisable to log out of the postgres user and run it as your regular user

systemctl status postgresql.service

From the information obtained you can draw conclusions about the causes of the problem

● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Thu 2021-06-10 01:29:02 EEST; 49s ago Process: 12977 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=1/FAILURE) Process: 12970 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS) Jun 10 01:29:01 host.andrei.com systemd[1]: Starting PostgreSQL database server... Jun 10 01:29:01 host.andrei.com pg_ctl[12977]: pg_ctl: another server might be running; trying to start server anyway Jun 10 01:29:01 host.andrei.com pg_ctl[12977]: LOG: syntax error in file "/var/lib/pgsql/data/postgresql.conf" line 192, near token "/" Jun 10 01:29:01 host.andrei.com pg_ctl[12977]: FATAL: configuration file "/var/lib/pgsql/data/postgresql.conf" contains errors Jun 10 01:29:02 host.andrei.com systemd[1]: postgresql.service: control process exited, code=exited status=1 Jun 10 01:29:02 host.andrei.com systemd[1]: Failed to start PostgreSQL database server. Jun 10 01:29:02 host.andrei.com systemd[1]: Unit postgresql.service entered failed state. Jun 10 01:29:02 host.andrei.com systemd[1]: postgresql.service failed.

There is an error in the file postgresql.conf

The location of the configuration file postgresql.conf can be obtained by running

-bash-4.2$ su - postgres -c "psql -c 'SHOW config_file;'"

Password: config_file ------------------------------------- /var/lib/pgsql/data/postgresql.conf (1 row)

Now you need to substitute the result you obtained into vi

vi /var/lib/pgsql/data/postgresql.conf

And fix the error

Banner image

Another example

● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Thu 2021-06-10 01:33:42 EEST; 28s ago Process: 13262 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=1/FAILURE) Process: 13257 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS) Jun 10 01:33:41 host.andrei.com systemd[1]: Starting PostgreSQL database server... Jun 10 01:33:41 host.andrei.com pg_ctl[13262]: pg_ctl: another server might be running; trying to start server anyway Jun 10 01:33:41 host.andrei.com pg_ctl[13262]: LOG: invalid value for parameter "wal_level": "replica" Jun 10 01:33:41 host.andrei.com pg_ctl[13262]: HINT: Available values: minimal, archive, hot_standby. Jun 10 01:33:41 host.andrei.com pg_ctl[13262]: FATAL: configuration file "/var/lib/pgsql/data/postgresql.conf" contains errors Jun 10 01:33:42 host.andrei.com systemd[1]: postgresql.service: control process exited, code=exited status=1 Jun 10 01:33:42 host.andrei.com systemd[1]: Failed to start PostgreSQL database server. Jun 10 01:33:42 host.andrei.com systemd[1]: Unit postgresql.service entered failed state. Jun 10 01:33:42 host.andrei.com systemd[1]: postgresql.service failed.

Again there is an error in the file postgresql.conf the value of the parameter wal_level is not allowed for the current PostgreSQL version

For 9.2 you can choose from minimal, archive and hot_standby

You need to run

vi /var/lib/pgsql/data/postgresql.conf

And replace replica with hot_standby

Another example

● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Thu 2021-06-10 01:43:39 EEST; 6s ago Process: 13625 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=1/FAILURE) Process: 13620 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS) Jun 10 01:43:38 host.andrei.com systemd[1]: Starting PostgreSQL database server... Jun 10 01:43:38 host.andrei.com pg_ctl[13625]: pg_ctl: another server might be running; trying to start server anyway Jun 10 01:43:38 host.andrei.com pg_ctl[13625]: FATAL: lock file "postmaster.pid" already exists Jun 10 01:43:38 host.andrei.com pg_ctl[13625]: HINT: Is another postmaster (PID 25489) running in data directory "/var/lib/pgsql/data"? Jun 10 01:43:39 host.andrei.com systemd[1]: postgresql.service: control process exited, code=exited status=1 Jun 10 01:43:39 host.andrei.com systemd[1]: Failed to start PostgreSQL database server. Jun 10 01:43:39 host.andrei.com systemd[1]: Unit postgresql.service entered failed state. Jun 10 01:43:39 host.andrei.com systemd[1]: postgresql.service failed.

At least one more server is running.

You can check what is currently in postmaster.pid with the command

cat postmaster.pid

25489 /var/lib/pgsql/data 1623237280 5432 /var/run/postgresql localhost 5432001 45

You can also search for postgres in the ps output

ps -A | grep postgres

25489 pts/1 00:00:00 postgres 25490 ? 00:00:00 postgres 25492 ? 00:00:00 postgres 25493 ? 00:00:00 postgres 25494 ? 00:00:00 postgres 25495 ? 00:00:01 postgres 25496 ? 00:00:00 postgres

kill 25489
systemctl stop postgresql.service
systemctl start postgresql.service
systemctl status postgresql.service

[andrei@host lib]$ systemctl status postgresql.service ● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2021-06-10 02:21:42 EEST; 2s ago Process: 14910 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=0/SUCCESS) Process: 14905 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS) Main PID: 14913 (postgres) Tasks: 7 CGroup: /system.slice/postgresql.service ├─14913 /usr/bin/postgres -D /var/lib/pgsql/data -p 5432 ├─14914 postgres: logger process ├─14916 postgres: checkpointer process ├─14917 postgres: writer process ├─14918 postgres: wal writer process ├─14919 postgres: autovacuum launcher process └─14920 postgres: stats collector process Jun 10 02:21:41 host.andrei.com systemd[1]: Starting PostgreSQL database server... Jun 10 02:21:42 host.andrei.com systemd[1]: Started PostgreSQL database server.

Article author: Andrei Olegovich