PostgreSQL 9.5 Service Exits Immediately After Startup: Debugging Systemd and Manual Launch Scenarios


4 views

When examining the service status with systemctl status postgresql, we observe:

Active: active (exited) since Fr 2016-12-02 11:02:51 CET
Main PID: 2360 (code=exited, status=0/SUCCESS)

The critical detail here is the active (exited) state, indicating the service completes its execution chain but doesn't maintain a running process. This contrasts with the successful manual start:

sudo -u postgres /usr/lib/postgresql/9.5/bin/postgres -d 3 -D /var/lib/postgresql/9.5/main/ -c config_file=/etc/postgresql/9.5/main/postgresql.conf

The problematic service file (/lib/systemd/system/postgresql.service) shows unusual configuration:

[Service]
Type=oneshot
ExecStart=/bin/true
ExecReload=/bin/true
RemainAfterExit=on

Key issues identified:

  • The oneshot type indicates a service that should exit after execution
  • /bin/true explains the immediate successful exit
  • Missing actual PostgreSQL startup command

Create a proper service override:

sudo systemctl edit postgresql

Add these contents:

[Service]
Type=forking
ExecStart=
ExecStart=/usr/bin/pg_ctlcluster 9.5 main start
ExecStop=
ExecStop=/usr/bin/pg_ctlcluster 9.5 main stop
Restart=on-failure

For multi-cluster systems, use the template service:

sudo systemctl start postgresql@9.5-main

This leverages the proper service definition from postgresql@.service which includes the correct pg_ctlcluster commands.

Check the journal for detailed errors:

journalctl -u postgresql@9.5-main -xe

Key things to verify:

  • Data directory permissions (/var/lib/postgresql/9.5/main)
  • Configuration file ownership (/etc/postgresql/9.5/main)
  • Available memory (notice the HugeTLB error in manual output)

When logs remain empty, try direct execution with debug flags:

sudo -u postgres /usr/lib/postgresql/9.5/bin/postgres \
    -D /var/lib/postgresql/9.5/main \
    -c config_file=/etc/postgresql/9.5/main/postgresql.conf \
    -c log_statement=all -c log_min_messages=debug1

When running service postgresql start on Debian 8 with PostgreSQL 9.5, the service shows as active (exited) in systemd status while no actual PostgreSQL processes are running. Manual startup via postgres binary works perfectly, indicating a systemd configuration issue rather than a PostgreSQL problem.

The root cause lies in the default service file from postgresql-common package:


[Unit]
Description=PostgreSQL RDBMS

[Service]
Type=oneshot
ExecStart=/bin/true
ExecReload=/bin/true
RemainAfterExit=on

This configuration merely runs /bin/true as a placeholder rather than actually starting PostgreSQL. The postgresql@.service template is what actually manages individual clusters.

For Debian's multi-cluster setup, you should use:


# Start all clusters
sudo systemctl start postgresql@9.5-main

# Or for a specific cluster
sudo pg_ctlcluster 9.5 main start

Check which clusters are configured:


pg_lsclusters

Create a proper systemd service override:


sudo systemctl edit postgresql.service

[Service]
Type=forking
ExecStart=/usr/bin/pg_ctlcluster 9.5 main start
ExecStop=/usr/bin/pg_ctlcluster 9.5 main stop

If issues persist:

  1. Check logs in /var/log/postgresql/postgresql-9.5-main.log
  2. Verify permissions on data directory (/var/lib/postgresql/9.5/main)
  3. Test with debug mode: sudo -u postgres /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main -c config_file=/etc/postgresql/9.5/main/postgresql.conf -d 5

For production systems, consider creating a dedicated service file:


# /etc/systemd/system/postgresql-9.5.service
[Unit]
Description=PostgreSQL 9.5 Database Server
After=network.target

[Service]
Type=forking
User=postgres
ExecStart=/usr/lib/postgresql/9.5/bin/pg_ctl start -D /var/lib/postgresql/9.5/main -l /var/log/postgresql/postgresql-9.5-main.log -s -o "-c config_file=/etc/postgresql/9.5/main/postgresql.conf"
ExecStop=/usr/lib/postgresql/9.5/bin/pg_ctl stop -D /var/lib/postgresql/9.5/main -s -m fast
ExecReload=/usr/lib/postgresql/9.5/bin/pg_ctl reload -D /var/lib/postgresql/9.5/main -s
TimeoutSec=300

[Install]
WantedBy=multi-user.target