升级14.04至16.04.1后,PostgreSQL服务器无法启动。

刚刚将我的系统从14.04 LTS升级到16.04.1 LTS,systemd上的postgresql无法启动。
/etc/init.d/postgresql start                                                                                                                                                                              
[ ok ] Starting postgresql (via systemctl): postgresql.service

# /etc/init.d/postgresql status                                                                                                                                                                             
● postgresql.service - PostgreSQL RDBMS
  Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor     preset: enabled)
  Active: active (exited) since Вт 2016-08-09 13:40:51 MSK; 3min 23s ago
  Process: 23142 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
  Main PID: 23142 (code=exited, status=0/SUCCESS)Seems that 

似乎systemd脚本包含错误的数据:
# cat /lib/systemd/system/postgresql.service                                                                                                                                                                
# systemd service for managing all PostgreSQL clusters on the system. This
# service is actually a systemd target, but we are using a service since
# targets cannot be reloaded.

[Unit]
Description=PostgreSQL RDBMS

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

[Install]
WantedBy=multi-user.target

什么是适用于PostgreSQL的正确脚本?
4个回答

Systemd应该使用位于/etc/init.d的脚本。然而,systemd使用了某种模板文件。要修复这个问题,请按照以下步骤进行:

  1. 删除无效的服务脚本:

    # rm /lib/systemd/system/postgresql.service

  2. 重新加载守护进程脚本:

    # systemctl daemon-reload

  3. 启用postgresql服务:

    # systemctl enable postgresql

完成后,您可以使用任何首选的变体启动postgres:sudo systemctl start postgresqlsudo service postgresql startsudo /etc/init.d/postgresql start。要检查postgresql是否实际运行,请检查服务的状态:sudo systemctl status postgresql。它应该显示如下:

% sudo systemctl status postgresql                                                                                                                                                                          
● postgresql.service - LSB: PostgreSQL RDBMS server
   Loaded: loaded (/etc/init.d/postgresql; bad; vendor preset: enabled)
   Active: active (running) since Пт 2016-08-12 10:13:43 MSK; 1h 37min ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/postgresql.service
           ├─4086 /usr/lib/postgresql/9.5/bin/postgres -D     /var/lib/postgresql/9.5/main -c  config_file=/etc/postgresql/9.5/main/postgresql.conf
           ├─4099 postgres: checkpointer process                                                                                              
           ├─4100 postgres: writer process                                                                                                    
           ├─4101 postgres: wal writer process                                                                                                
           ├─4102 postgres: autovacuum launcher process                                                                                       
           └─4103 postgres: stats collector process                                                                                           

авг 12 10:13:40 ubuntu-precise systemd[1]: Starting LSB: PostgreSQL RDBMS server...
авг 12 10:13:40 ubuntu-precise postgresql[4009]:  * Starting PostgreSQL 9.5 database server
авг 12 10:13:43 ubuntu-precise postgresql[4009]:    ...done.
авг 12 10:13:43 ubuntu-precise systemd[1]: Started LSB: PostgreSQL RDBMS server.

1一定有错。Systemd确实工作。它一直很好地启动我的9.3集群;只是无法启动我的9.5集群。这不是一个无效的服务脚本,而是一个他们称之为可重载的目标脚本。丑陋的黑客似乎也不是正确的方法,但是应该可以通过systemctl _action_ postgresql@_version_来运行。 - Auspex
我不知道这是否是一个丑陋的解决方式(很可能是),但我在_postfix_上遇到了同样的问题,而这个答案似乎解决了我的问题。 - decibyte
在执行systemctl enable postgresql命令时,我遇到了以下错误:postgresql.service不是本地服务,正在重定向到systemd-sysv-install 执行/lib/systemd/systemd-sysv-install enable postgresql insserv: fopen(.depend.stop): 权限被拒绝 insserv: fopen(.depend.stop): 权限被拒绝。显然,这与我的设置不兼容。 - Kemin Zhou

如果您提供正确的版本和集群名称,这应该可以直接使用。

假设您正在运行版本9.5,并且集群被称为main

启动:systemctl start postgresql@9.5-main

停止:systemctl stop postgresql@9.5-main

状态:systemctl status postgresql@9.5-main

开机自动启动:systemctl enable postgresql@9.5-main

禁止开机自动启动:systemctl disable postgresql@9.5-main

foo@postgres:~$ systemctl status postgresql@9.5-main
●  postgresql@9.5-main.service - PostgreSQL Cluster 9.5-main
   Loaded: loaded (/lib/systemd/system/postgresql@.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2017-03-31 17:44:46 CEST; 59s ago
   Main PID: 4546 (postgres)
   CGroup: /system.slice/system-postgresql.slice/postgresql@9.5-main.service
           ├ ─ 4546 /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main -c config_file=/etc/postgresql/9.5/main/postgresql.conf
           ├ ─ 4548 postgres: checkpointer process
           ├ ─ 4549 postgres: writer process
           ├ ─ 4550 postgres: wal writer process
           ├ ─ 4551 postgres: autovacuum launcher process
           └ ─ 4552 postgres: stats collector process

Mar 31 17:44:44 postgres postgres[4546]: [1-2] 2017-03-31 17:44:44 CEST [4546] @ HINT:  Future log output will go to log destination "syslog".
Mar 31 17:44:44 postgres postgres[4547]: [2-1] 2017-03-31 17:44:44 CEST [4547] @ LOG:  database system was shut down at 2017-03-31 17:44:43 CEST
Mar 31 17:44:44 postgres postgres[4547]: [3-1] 2017-03-31 17:44:44 CEST [4547] @ LOG:  MultiXact member wraparound protections are now enabled
Mar 31 17:44:44 postgres postgres[4546]: [2-1] 2017-03-31 17:44:44 CEST [4546] @ LOG:  database system is ready to accept connections
Mar 31 17:44:44 postgres postgres[4551]: [2-1] 2017-03-31 17:44:44 CEST [4551] @ LOG:  autovacuum launcher started
Mar 31 17:44:45 postgres postgres[4553]: [3-1] 2017-03-31 17:44:45 CEST [4553] [unknown]@[unknown] LOG:  incomplete startup packet
Mar 31 17:44:46 postgres systemd[1]: Started PostgreSQL Cluster 9.5-main.
Mar 31 17:44:47 postgres systemd[1]: Reloading PostgreSQL Cluster 9.5-main.
Mar 31 17:44:47 postgres postgres[4546]: [3-1] 2017-03-31 17:44:47 CEST [4546] @ LOG:  received SIGHUP, reloading configuration files
Mar 31 17:44:47 systemd[1]: Reloaded PostgreSQL Cluster 9.5-main.

可以证明这是有效的工作。我该如何设置systemd以实现自动启动呢? - Artur Eshenbrener
1systemctl enable postgresql@9.5-main 就是你所需要的。 - malte
1刚刚将PostgreSQL从9.6升级到10.0,又遇到了这个问题。尝试了你的解决方案并证明:它有效。 - Artur Eshenbrener
从Kemin的回答中提取:您可以使用例如systemctl edit postgresql@9.6-main来创建一个带有systemd指令的覆盖文件,如下所示: [Service] Nice=15 IOSchedulingClass=2 IOSchedulingPriority=7 - GreenReaper

我在将postges9清除并在我的ubuntu16上安装10之后遇到了同样的问题。最初,我编辑了/lib/systemd/system/postgresql.service文件,内容如下:
[Unit]
Description=PostgreSQL RDBMS

[Service]
Type=notify
User=postgres
ExecStart=/usr/lib/postgresql/10/bin/postgres -D /analysis2/postgresql/data
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0

[Install]
WantedBy=multi-user.target

我可以使用systemctl restart/stop/start命令来控制postgresql服务。但是,由于某种原因,上述文件被覆盖了(可能是系统更新后),我不能再运行systemctl命令来启动和停止postgresql服务器。经过一些阅读,我意识到你不应该编辑上述文件。相反,你应该使用systemctl edit foo来覆盖默认设置。按照以下链接提出的解决方案,我的系统似乎与postgresql正常工作。 如何覆盖或配置systemd服务?

不是最好的解决方案,但这样可以解决问题而无需深入研究任何内容。将此添加到cron中:
@reboot sleep 5; systemctl start postgresql