Ubuntu – How to force PostgreSQL to start after network is setup on Ubuntu Server 16

bootnetworkingpostgresqlUbuntu

PostgreSQL fails to bind socket because it starts before network is setup.

I use Ubuntu Server 16.04.1 LTS and PostgreSQL 9.5. I have read many docs over the internet but nothing solved my problem.

I am pretty sure that the problem is: PostgreSQL service try to create a socket before Network Interface is properly setup.

sudo systemctl status postgresql@9.5-main.service
● postgresql@9.5-main.service - PostgreSQL Cluster 9.5-main
   Loaded: loaded (/lib/systemd/system/postgresql@.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2016-07-27 09:42:36 UTC; 1min 43s ago
  Process: 5274 ExecStart=postgresql@%i --skip-systemctl-redirect %i start (code=exited, status=1/FAILURE)

Jul 27 09:42:34 airpy-server systemd[1]: Starting PostgreSQL Cluster 9.5-main...
Jul 27 09:42:36 airpy-server postgresql@9.5-main[5274]: The PostgreSQL server failed to start. Please check the log outp
Jul 27 09:42:36 airpy-server postgresql@9.5-main[5274]: 2016-07-27 09:42:36 UTC [5332-1] LOG:  could not bind IPv4 socke
Jul 27 09:42:36 airpy-server postgresql@9.5-main[5274]: 2016-07-27 09:42:36 UTC [5332-2] HINT:  Is another postmaster al
Jul 27 09:42:36 airpy-server postgresql@9.5-main[5274]: 2016-07-27 09:42:36 UTC [5332-3] WARNING:  could not create list
Jul 27 09:42:36 airpy-server postgresql@9.5-main[5274]: 2016-07-27 09:42:36 UTC [5332-4] FATAL:  could not create any TC
Jul 27 09:42:36 airpy-server systemd[1]: postgresql@9.5-main.service: Control process exited, code=exited status=1
Jul 27 09:42:36 airpy-server systemd[1]: Failed to start PostgreSQL Cluster 9.5-main.
Jul 27 09:42:36 airpy-server systemd[1]: postgresql@9.5-main.service: Unit entered failed state.
Jul 27 09:42:36 airpy-server systemd[1]: postgresql@9.5-main.service: Failed with result 'exit-code'.

I must use DHCP (and I cannot change it) with a fixed lease. Since I have set the listen_address='fixed_ip' into postgresql.conf the cluster does not start at boot. This is how everything started…

And I have wasted two days trying to make postgreSQL wait for Network configuration without success. It was working like a charm on Ubuntu Server 14.04 (I had nothing to configure) and now it just does not work for the new LTS release.

I have, at least, tried those leads:

  • Ubuntu-Forum, it removed another errors (A start job is running for Raise of network interface…)
  • NetworkTarget, it added other errors
    And my problem remains.

How can I force PostgreSQL to start after network is setup on Ubuntu Server 16?

Thank you for your help

Best Answer

Using new systemctl service manager, you can force service to wait for network is active until it starts. This is achieved by editing the service unit file (look in /etc/systemd/system/) and adding following lines in unit section:

[Unit]

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

Then reload service with the new configuration:

systemctl reload name.service

Reboot the system to check it wait for network before starting service.