Ubuntu – PostgreSQL fails to reinstall after upgrading Ubuntu 12.04 to 14.04

12.0414.04postgresqlupgrade

I ran do-release-upgrade to update an Ubuntu 12.04 server to Ubuntu 14.04. It seemed to go smoothly, but when I did apt-get update after the upgrade I notice the PostgreSQL packages were still looking at the precise repos instead of the trusty repos. I followed the instructions here to add the trusty repos and moved the .list file containing the precise repos to a temporary directory as a backup. After I did that, sudo apt-get upgrade failed and I was not able to get it to run completely again even after restoring the precise repos. I did not happen to grab the error messages. Since this is a development server and all the data is non-essential, I decided just to re-install PostgreSQL. This repeatedly failed after several attempts to clear out any custom configuration we may have had on the server that I thought might be interfering. I am able to do sudo apt-get install postgresql-common successfully, but if I try sudo apt-get install postgresql-9.5 it fails with the following:

Setting up postgresql-9.5 (9.5.5-1.pgdg14.04+1) ...
Creating new cluster 9.5/main ...
  config /etc/postgresql/9.5/main
  data   /var/lib/postgresql/9.5/main
  locale en_US.UTF-8
  socket /var/run/postgresql
  port   5432
update-alternatives: using /usr/share/postgresql/9.5/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
 * Starting PostgreSQL 9.5 database server                                                                                             
 * Failed to issue method call: Unit postgresql@9.5-main.service failed to load: No such file or directory. See system logs and 'systemctl status postgresql@9.5-main.service' for details.
                                                                                                                               [fail]
invoke-rc.d: initscript postgresql, action "start" failed.
dpkg: error processing package postgresql-9.5 (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of postgresql-contrib-9.5:
 postgresql-contrib-9.5 depends on postgresql-9.5 (= 9.5.5-1.pgdg14.04+1); however:
  Package postgresql-9.5 is not configured yet.

dpkg: error processing package postgresql-contrib-9.5 (--configure):
 dependency problems - leaving unconfigured
Setting up sysstat (10.2.0-1) ...
No apport report written because the error message indicates its a followup error from a previous failure.
                                                                                                          update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Processing triggers for libc-bin (2.19-0ubuntu6.9) ...
Errors were encountered while processing:
 postgresql-9.5
 postgresql-contrib-9.5
E: Sub-process /usr/bin/dpkg returned an error code (1)

Jan 17 15:31:31 beta kernel: [    4.029504] systemd-journald[543]: Failed to resolve 'systemd-journal' group: No such process

If I try systemctl status postgresql@9.5-main.service I get:

postgresql@9.5-main.service
   Loaded: error (Reason: No such file or directory)
   Active: inactive (dead)

The /var/log/postgresql/postgresql-9.5-main.log log file is empty, so I'm not sure where else to check for logs. After failing to install, I go through the steps here to do a complete uninstallation. I can't tell if this an issue with PostgreSQL or the upgraded server.

UPDATE: The output of dpkg -l "postgresql*":

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                         Version             Architecture        Description
+++-============================-===================-===================-==============================================================
un  postgresql-7.4               <none>              <none>              (no description available)
un  postgresql-8.0               <none>              <none>              (no description available)
un  postgresql-9.1               <none>              <none>              (no description available)
iF  postgresql-9.5               9.5.5-1.pgdg14.04+1 amd64               object-relational SQL database, version 9.5 server
un  postgresql-client            <none>              <none>              (no description available)
ii  postgresql-client-9.5        9.5.5-1.pgdg14.04+1 amd64               front-end programs for PostgreSQL 9.5
ii  postgresql-client-common     178.pgdg14.04+1     all                 manager for multiple PostgreSQL client versions
ii  postgresql-common            178.pgdg14.04+1     all                 PostgreSQL database-cluster manager
iU  postgresql-contrib-9.5       9.5.5-1.pgdg14.04+1 amd64               additional facilities for PostgreSQL
un  postgresql-doc-9.5           <none>              <none>              (no description available)

Best Answer

I had the same problem, upgraded Ubuntu 12.04 to 14.04. Don't know but it also using systemd. While checking /etc/init.d/postgresql file it uses /usr/share/postgresql-common/init.d-functions.

This file executes /usr/bin/pg_ctlcluster to start and stop postgresql server.

if [ "$1" = "stop" ] || [ "$1" = "restart" ]; then
    ERRMSG=$(pg_ctlcluster --force "$2" "$name" $1 2>&1)
else
    ERRMSG=$(pg_ctlcluster "$2" "$name" $1 2>&1)
fi

/usr/bin/pg_ctlcluster file accepts --skip-systemctl-redirect option to start or stop postgresql without systectl.

So you need to add --skip-systemctl-redirect in /usr/share/postgresql-common/init.d-functions in do_ctl_all() function. So it will look like this.

if [ "$1" = "stop" ] || [ "$1" = "restart" ]; then
    ERRMSG=$(pg_ctlcluster --skip-systemctl-redirect --force "$2" "$name" $1 2>&1)
else
    ERRMSG=$(pg_ctlcluster --skip-systemctl-redirect "$2" "$name" $1 2>&1)
fi

Or you can add $skip_systemctl_redirect = 1; before $skip_systemctl_redirect is checked in /usr/bin/pg_ctlcluster.

Related Question