温馨提示×

PostgreSQL如何在CentOS启动

小樊
37
2026-08-31 03:23:32
栏目: 云计算

CentOS 上启动 PostgreSQL,常见场景分为 CentOS 7+(systemd)CentOS 6(init.d)。下面以 CentOS 7 / 8 / Stream 为主(目前最常见)。


一、确认 PostgreSQL 是否已安装

psql --version

rpm -qa | grep postgres

如果未安装,可先安装(以 PostgreSQL 14 为例):

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql14-server

二、初始化数据库(仅首次安装需要)

sudo /usr/pgsql-14/bin/postgresql-14-setup initdb

不同版本路径可能不同,如 postgresql-15


三、启动 PostgreSQL(systemd)

1️⃣ 启动服务

sudo systemctl start postgresql-14

2️⃣ 设置开机自启

sudo systemctl enable postgresql-14

3️⃣ 查看运行状态

sudo systemctl status postgresql-14

✅ 看到 active (running) 表示启动成功


四、使用默认 PostgreSQL 用户连接

PostgreSQL 默认使用 postgres 系统用户:

sudo -i -u postgres
psql

或:

sudo -u postgres psql

五、常见问题排查

1️⃣ 启动失败

查看日志:

sudo journalctl -xe -u postgresql-14

或:

cat /var/lib/pgsql/14/data/log/*.log

2️⃣ 端口未监听(默认 5432)

ss -lntp | grep 5432

如未监听,检查:

/var/lib/pgsql/14/data/postgresql.conf

确认:

listen_addresses = '*'
port = 5432

3️⃣ 远程连接失败

编辑 pg_hba.conf

/var/lib/pgsql/14/data/pg_hba.conf

添加:

host  all  all  0.0.0.0/0  md5

然后重启:

sudo systemctl restart postgresql-14

六、CentOS 6(老系统,init.d)

service postgresql initdb
service postgresql start
chkconfig postgresql on

七、快速自检清单 ✅

  • [ ] PostgreSQL 已安装
  • [ ] 数据库已初始化
  • [ ] systemctl start 成功
  • [ ] psql 可连接

如果你愿意,可以告诉我:

  • ✅ CentOS 版本(cat /etc/centos-release
  • ✅ PostgreSQL 版本
  • ✅ 是 本地启动 还是 远程连接失败

我可以给你更精确的解决方案。

0