以下是 CentOS 7 系统中 PostgreSQL 服务管理的常用命令:
1. 检查 PostgreSQL 服务状态
systemctl status postgresql
如果安装了特定版本(例如 PostgreSQL 14),需要指定版本号:
systemctl status postgresql-14
2. 启动 PostgreSQL 服务
systemctl start postgresql
指定版本:
systemctl start postgresql-14
3. 停止 PostgreSQL 服务
systemctl stop postgresql
指定版本:
systemctl stop postgresql-14
4. 重启 PostgreSQL 服务
systemctl restart postgresql
指定版本:
systemctl restart postgresql-14
5. 重载配置文件
当修改了配置文件(如 postgresql.conf
或 pg_hba.conf
)后,可以使用以下命令重载配置而不重启服务:
systemctl reload postgresql
指定版本:
systemctl reload postgresql-14
6. 设置开机自启
systemctl enable postgresql
指定版本:
systemctl enable postgresql-14
7. 禁用开机自启
systemctl disable postgresql
指定版本:
systemctl disable postgresql-14
8. 查看 PostgreSQL 运行端口
查看 PostgreSQL 是否在监听(默认端口 5432):
netstat -tulnp | grep 5432
或者:
ss -tunlp | grep 5432
9. 查看服务日志
journalctl -u postgresql
指定版本:
journalctl -u postgresql-14
10. 初始化数据库目录
首次安装 PostgreSQL 后需要初始化数据库目录:
postgresql-setup initdb
指定版本:
postgresql-setup --initdb --unit postgresql-14
11. 配置防火墙允许访问 PostgreSQL
如果需要远程访问 PostgreSQL,需要开放端口 5432:
添加防火墙规则
firewall-cmd --permanent --add-port=5432/tcp
重新加载防火墙
firewall-cmd --reload
查看端口规则
firewall-cmd --list-ports
这些命令涵盖了 PostgreSQL 服务管理的主要操作,适用于 CentOS 7 系统下的 PostgreSQL 管理场景。