在 CentOS 中完成其他答案。
运行之后
$ initdb -D /path/to/newdb
您必须至少更改 port 配置选项,并且可能在配置文件 postgresql.conf 中更改 listen_addresses。
您可能希望新实例在系统启动时自动运行(例如在关机的情况下),而不是立即启动这个新实例,这已在之前的答案中进行了解释。为此,由于 CentOS 没有 pg_ctl register 选项(仅适用于 Windows),您必须创建一个新的服务文件并注册它,以便 systemctl 或服务可以自动启动它。
CentOS 6
[root@machine ~]# service postgresql-9.6 edit
Usage: /etc/init.d/postgresql-9.6 {start|stop|status|restart|upgrade|condrestart|try-restart|reload|force-reload|initdb|promote}
[root@machine ~]# cd /etc/init.d # Now we know where service file is
[root@machine init.d]# cp -p postgresql-9.6 postgresql-9.6_5433
[root@machine init.d]# vi postgresql-9.6_5433
现在您可以将PGDATA 目录更改为新实例所在的目录。如果您使用的是 9.4 之前的 Postgresql 版本(在此答案时您应该这样做),您还必须将 PGPORT 更改为新实例正在侦听的值。
新服务的名称由您决定。我一般取原来的服务名,最后加上端口号。
现在您只需注册新服务:
[root@machine init.d]# chkconfig postgresql-9.6_5433 on # service registered!
[root@machine init.d]# service postgresql-9.6_5433 start
Iniciando servicios postgresql-9.6_5433: [ OK ]
[root@machine init.d]# service postgresql-9.6_5433 status
Se está ejecutando postgresql-9.6_5433 (pid 120993)...
在 CentOS 7 中,而不是 service 来控制在您拥有 systemctl 的机器上运行的服务,并且命令和路径会发生一些变化。但过程是一样的:创建新的服务文件,使用新的位置/端口进行编辑,注册并启动:
[root@localhost ~]# locate postgresql.service
/etc/systemd/system/multi-user.target.wants/postgresql.service
/usr/lib/systemd/system/postgresql.service
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost ~]# cp -p postgresql.service postgresql_5433.service
[root@localhost ~]# vi postgresql_5433.service
# Change PGDATA and maybe PGPORT if PG version <9.4
[root@localhost ~]# systemctl enable postgresql_5433.service
[root@localhost ~]# systemctl start postgresql_5433.service
[root@localhost ~]# systemctl list-unit-files | grep postgres
postgresql.service enabled
postgresql_5433.service enabled