Fedora 37服务器能否部署两个不同版本PostgreSQL及配置文件位置
PostgreSQL多版本实例独立配置(Fedora 37环境)
完全可以实现不同PostgreSQL版本实例使用独立配置文件、监听不同端口,具体操作步骤如下:
1. 创建独立的数据与配置目录
为每个版本的实例创建专属的数据目录和配置目录,避免与默认实例冲突:
- 示例路径:
- PostgreSQL 11:数据目录
/var/lib/pgsql/11-custom,配置目录/etc/pgsql/11-custom - PostgreSQL 15:数据目录
/var/lib/pgsql/15-custom,配置目录/etc/pgsql/15-custom
- PostgreSQL 11:数据目录
- 设置目录权限(确保postgres用户拥有读写权限):
chown -R postgres:postgres /var/lib/pgsql/11-custom /etc/pgsql/11-custom chown -R postgres:postgres /var/lib/pgsql/15-custom /etc/pgsql/15-custom
2. 初始化对应版本的实例
使用对应版本的initdb工具初始化实例,生成基础配置文件:
- PostgreSQL 11:
su - postgres -c "/usr/pgsql-11/bin/initdb -D /var/lib/pgsql/11-custom -A scram-sha-256" - PostgreSQL 15:
su - postgres -c "/usr/pgsql-15/bin/initdb -D /var/lib/pgsql/15-custom -A scram-sha-256"
执行后,数据目录下会生成默认的postgresql.conf和pg_hba.conf,你可以直接修改这些文件,或者将它们移动到之前创建的自定义配置目录中。
3. 修改配置参数
编辑对应版本的配置文件,设置独立端口和其他所需参数:
- 打开
postgresql.conf,修改监听端口:- PostgreSQL 11:
port = 5432(或其他未被占用的端口) - PostgreSQL 15:
port = 5433(与11版本端口区分开)
- PostgreSQL 11:
- 按需调整
listen_addresses参数(例如设为'*'允许所有IP访问),同时在pg_hba.conf中配置符合需求的访问控制规则。
4. 启动自定义实例(两种方式)
方式一:命令行直接启动
通过命令行参数指定自定义配置文件路径:
- PostgreSQL 11:
su - postgres -c "/usr/pgsql-11/bin/postgres -D /var/lib/pgsql/11-custom -c config_file=/etc/pgsql/11-custom/postgresql.conf -c hba_file=/etc/pgsql/11-custom/pg_hba.conf" - PostgreSQL 15:
su - postgres -c "/usr/pgsql-15/bin/postgres -D /var/lib/pgsql/15-custom -c config_file=/etc/pgsql/15-custom/postgresql.conf -c hba_file=/etc/pgsql/15-custom/pg_hba.conf"
方式二:创建systemd服务(推荐,便于管理)
复制默认服务文件并修改,实现开机自启和服务管理:
- 复制并编辑PostgreSQL 11的自定义服务文件:
打开编辑该文件,修改cp /usr/lib/systemd/system/postgresql-11.service /etc/systemd/system/postgresql-11-custom.serviceEnvironment=PGDATA=/var/lib/pgsql/11-custom,并更新ExecStart行,添加配置文件指定参数:ExecStart=/usr/pgsql-11/bin/postgres -D ${PGDATA} -c config_file=/etc/pgsql/11-custom/postgresql.conf -c hba_file=/etc/pgsql/11-custom/pg_hba.conf - 对PostgreSQL 15执行相同操作,复制并修改服务文件为
postgresql-15-custom.service,替换对应路径和版本号。 - 重新加载systemd并启动服务:
systemctl daemon-reload systemctl start postgresql-11-custom postgresql-15-custom systemctl enable postgresql-11-custom postgresql-15-custom
5. 验证实例状态
使用对应版本的psql工具连接测试:
- 连接PostgreSQL 11:
/usr/pgsql-11/bin/psql -p 5432 -U postgres - 连接PostgreSQL 15:
/usr/pgsql-15/bin/psql -p 5433 -U postgres
能正常连接则说明配置生效。
内容的提问来源于stack exchange,提问作者zoran
相关产品推荐
相关产品推荐

