如何在不卸载 PostgreSQL 服务器的情况下禁用 1 版本

如何在不卸载 PostgreSQL 服务器的情况下禁用 1 版本

我已经在 Ubuntu 机器上安装了 PostgreSQL。postgresql9.1服务9.2启动两个版本:

$ sudo service postgresql start
$ * Starting PostgreSQL 9.1 database server  [ OK ]
$ * Starting PostgreSQL 9.2 database server  [ OK ]

我想仅启动9.2服务器但不卸载9.1,可以吗?

答案1

简洁版本:
替换automanual/etc/postgresql/9.1/main/start.conf

长版本

Debian/Ubuntu 中的每个 PostgreSQL 集群都有一个start.conf文件来控制/etc/init.d/postgresql应该做什么。

这是有记录的pg_createcluster

   STARTUP CONTROL
   The start.conf file in the cluster configuration directory controls the
   start/stop behavior of that cluster’s postmaster process. The file can
   contain comment lines (started with ’#’), empty lines, and must have
   exactly one line with one of the following keywords:

   auto
       The postmaster process is started/stopped automatically in the init
       script.  This is also the default if the file is missing.

   manual
       The postmaster process is not handled by the init script, but
       manually controlling the cluster with pg_ctlcluster(1) is
       permitted.

   disable
       Neither the init script nor pg_ctlcluster(1) are permitted to
       start/stop the cluster. Please be aware that this will not stop the
       cluster owner from calling lower level tools to control the
       postmaster process; this option is only meant to prevent accidents
       during maintenance, not more.

答案2

  1. 变体:将 postgresql 集群设置为手动并禁用 postgresql 服务。

1.1.将postgresql集群设置为手动模式:

robert@debian-pg:~$ sudo sed -i 's/^auto$/manual/' /etc/postgresql/12/prod/start.conf

1.2.检查是否完成:

robert@debian-pg:~$ cat /etc/postgresql/12/prod/start.conf
# Automatic startup configuration
#   auto: automatically start the cluster
#   manual: manual startup with pg_ctlcluster/[email protected] only
#   disabled: refuse to start cluster
# See pg_createcluster(1) for details. When running from systemd,
# invoke 'systemctl daemon-reload' after editing this file.

manual

1.3. 编辑后您必须更新:

robert@debian-pg:~$ sudo systemctl daemon-reload

执行此命令后,服务会重新读取start.conf并更新其配置,因此无需强制禁用它(第 1.5 节)。您可以检查一下:

robert@debian-pg:~$ sudo systemctl is-enabled [email protected]
disabled

1.4.停止postgresql集群服务:

robert@debian-pg:~$ systemctl stop postgresql@[email protected]

1.5. 如果您错过了第 1.3 节,或者无法禁用 postgresql 服务:

robert@debian-pg:~$ systemctl disable postgresql@[email protected]

它是什么样子的:

robert@debian-pg:~$ sudo systemctl is-enabled [email protected]
disabled

robert@debian-pg:~$ sudo systemctl is-enabled postgresql.service
enabled

robert@debian-pg:~$ sudo systemctl is-enabled [email protected]
indirect

PostgreSQL 集群 13-main 自动启动,与 PostgreSQL 12-prod 不同:

robert@debian-pg:~$ sudo systemctl is-enabled [email protected]
enabled-runtime

robert@debian-pg:~$ tail -1 /etc/postgresql/13/main/start.conf
auto

PostgreSQL 12-prod 自动运行已禁用:

robert@debian-pg:~$ sudo systemctl status [email protected][email protected] - PostgreSQL Cluster 12-prod
     Loaded: loaded (/lib/systemd/system/[email protected]; disabled;     vendor preset: enabled)
     Active: inactive (dead)

PostgreSQL 12-prod 处于手动模式:

robert@debian-pg:~$ cat /etc/postgresql/12/prod/start.conf
# Automatic startup configuration
#   auto: automatically start the cluster
#   manual: manual startup with pg_ctlcluster/[email protected] only
#   disabled: refuse to start cluster
# See pg_createcluster(1) for details. When running from systemd,
# invoke 'systemctl daemon-reload' after editing this file.

manual

要手动启动 postgresql 12-prod 集群,只需:

robert@debian-pg:~$ sudo systemctl start [email protected]

robert@debian-pg:~$ sudo systemctl status [email protected][email protected] - PostgreSQL Cluster 12-prod
     Loaded: loaded (/lib/systemd/system/[email protected]; disabled;     vendor preset: enabled)
     Active: active (running) since Mon 2024-01-22 10:33:18 MSK; 4s ago
    Process: 1516 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-    redirect 12-prod start (code=exited, status=0/SUCCESS)
   Main PID: 1521 (postgres)
      Tasks: 7 (limit: 2307)
     Memory: 25.5M
        CPU: 1.391s
     CGroup: /system.slice/system-postgresql.slice/postgresql@12-    prod.service
             ├─1521 /usr/lib/postgresql/12/bin/postgres -D     /var/lib/postgresql/12/prod -c     config_file=/etc/postgresql/12/prod/postgresql.conf
             ├─1524 postgres: 12/prod: checkpointer
             ├─1525 postgres: 12/prod: background writer
             ├─1526 postgres: 12/prod: walwriter
             ├─1527 postgres: 12/prod: autovacuum launcher
             ├─1528 postgres: 12/prod: stats collector
             └─1529 postgres: 12/prod: logical replication launcher

Jan 22 10:33:13 debian-pg systemd[1]: Starting PostgreSQL Cluster 12-    prod...
Jan 22 10:33:18 debian-pg systemd[1]: Started PostgreSQL Cluster 12-prod.

答案3

将 /etc/postgresql/9.1/main/posgtgresql.conf 重命名为其他名称,或将整个 /etc/postgresql/9.1 目录移动到其他位置。

启动脚本由 /etc/postgresql/ 中含有 */postgresql.conf 的目录决定。

相关内容