Search code examples
linuxpostgresqlsystemd

Postgresql12.3 wont start on boot, systemd


I installed Postgres 12.3 from source code with steps(according to this):

./configure --with-openssl --with-systemd
make
sudo make install

If I start with pg_ctl from postgres user all works fine: pg_ctl -D $PGDATA -l /path/to/logfile

Then I try to create a systemd service, as described here. Steps:

  1. Create file /etc/systemd/system/postgresql.service with content:
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)

[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /path/to/pgdata
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0

[Install]
WantedBy=multi-user.target
  1. sudo systemctl enable postgresql.service
  2. Then I reboot my machine. After restart Postgres unavaliable. Some logs: sudo systemctl status postgresql.service
postgresql.service - PostgreSQL database server
   Loaded: loaded (/etc/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2020-06-05 03:23:32 MSK; 37s ago
     Docs: man:postgres(1)
  Process: 724 ExecStart=/usr/local/pgsql/bin/postgres -D /path/to/pgdata (code=exited, status=1/FAILURE)
 Main PID: 724 (code=exited, status=1/FAILURE)

Jun 05 03:23:31 ctsvc systemd[1]: Starting PostgreSQL database server...
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Main process exited, code=exited, status=1/FAILURE
Jun 05 03:23:32 ctsvc systemd[1]: Failed to start PostgreSQL database server.
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Unit entered failed state.
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Failed with result 'exit-code'.

journalctl -xe | grep postgres

-- Subject: Unit postgresql.service has begun start-up
-- Unit postgresql.service has begun starting up.
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.209 MSK [724] LOG:  starting PostgreSQL 12.3 on armv7l-unknown-linux-gnueabihf, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 32-bit
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.211 MSK [724] LOG:  could not bind IPv4 address "172.17.17.42": Cannot assign requested address
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.211 MSK [724] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.211 MSK [724] WARNING:  could not create listen socket for "172.17.17.42"
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.211 MSK [724] FATAL:  could not create any TCP/IP sockets
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.212 MSK [724] LOG:  database system is shut down
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Main process exited, code=exited, status=1/FAILURE
-- Subject: Unit postgresql.service has failed
-- Unit postgresql.service has failed.
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Unit entered failed state.
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Failed with result 'exit-code'.
Jun 05 03:24:09 ctsvc sudo[1602]:   user1 : TTY=pts/0 ; PWD=/home/user1 ; USER=root ; COMMAND=/bin/systemctl status postgresql.service

netstat -tnl | grep "5432" - shows nothing. After that I can manualy run this service: sudo systemctl status postgresql.service

● postgresql.service - PostgreSQL database server
   Loaded: loaded (/etc/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-06-05 03:30:57 MSK; 8s ago
     Docs: man:postgres(1)
 Main PID: 1681 (postgres)
    Tasks: 8 (limit: 4915)
   CGroup: /system.slice/postgresql.service
           ├─1681 /usr/local/pgsql/bin/postgres -D /path/to/pgdata
           ├─1683 postgres: checkpointer   
           ├─1684 postgres: background writer   
           ├─1685 postgres: walwriter   
           ├─1686 postgres: autovacuum launcher   
           ├─1687 postgres: stats collector   
           ├─1688 postgres: logical replication launcher   
           └─1693 postgres: postgres postgres 172.17.17.40(53600) idle

Jun 05 03:30:56 ctsvc systemd[1]: Starting PostgreSQL database server...
Jun 05 03:30:57 ctsvc postgres[1681]: 2020-06-05 03:30:57.006 MSK [1681] LOG:  starting PostgreSQL 12.3 on armv7l-unknown-linux-gnueabihf, compiled b
Jun 05 03:30:57 ctsvc postgres[1681]: 2020-06-05 03:30:57.007 MSK [1681] LOG:  listening on IPv4 address "172.17.17.42", port 5432
Jun 05 03:30:57 ctsvc postgres[1681]: 2020-06-05 03:30:57.032 MSK [1681] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
Jun 05 03:30:57 ctsvc postgres[1681]: 2020-06-05 03:30:57.424 MSK [1682] LOG:  database system was shut down at 2020-06-05 02:59:03 MSK
Jun 05 03:30:57 ctsvc postgres[1681]: 2020-06-05 03:30:57.725 MSK [1681] LOG:  database system is ready to accept connections
Jun 05 03:30:57 ctsvc systemd[1]: Started PostgreSQL database server.
netstat -tnl | grep '5432'
tcp        0      0 172.17.17.42:5432       0.0.0.0:*               LISTEN

In my postgresql.conf I have following:

# - Connection Settings -
listen_addresses = '172.17.17.42'
port = 5432
max_connections = 100

If it helps: Postgres runs on Cubietruck with Armbian.

uname -a
Linux ctsvc 4.19.62-sunxi #5.92 SMP Wed Jul 31 22:07:23 CEST 2019 armv7l GNU/Linux

In my system there are no more processes that try to bind this port at boot time. As far as I understand, with the service itself and Postgresql everything is fine. However, something strange happens during the launch, but I can’t understand how to find out the reason of this behavior. Thanks in advance.


Solution

  • Finally my file /etc/systemd/system/postgresql.service looks like this:

    [Unit]
    Description=PostgreSQL database server
    Documentation=man:postgres(1)
    Wants=network-online.target
    After=network.target network-online.target
    
    [Service]
    Type=notify
    User=postgres
    ExecStart=/usr/local/pgsql/bin/postgres -D /path/to/pgdata
    ExecReload=/bin/kill -HUP $MAINPID
    KillMode=mixed
    KillSignal=SIGINT
    TimeoutSec=0
    
    [Install]
    WantedBy=multi-user.target
    

    Thanks to Laurenz Albe comment, I added following in Unit section:

    Wants=network-online.target
    After=network.target network-online.target
    

    to make sure that network fully operational before PG start. After this PG running correctly after reboot.