下面以 CentOS 7 / 8 / Stream 为例,给你一套从安装到基本配置的完整 PostgreSQL 设置流程(以 PostgreSQL 14 为例,其他版本类似)。
CentOS 默认仓库版本较旧,建议使用官方源。
# CentOS 7
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# CentOS 8 / Stream
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# CentOS 7
sudo yum install -y postgresql14-server postgresql14-contrib
# CentOS 8 / Stream
sudo dnf install -y postgresql14-server postgresql14-contrib
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14
sudo systemctl status postgresql-14
PostgreSQL 默认使用 peer 认证,先切换用户:
sudo -i -u postgres
psql
在 psql 中修改密码:
ALTER USER postgres WITH PASSWORD 'StrongPassword123';
\q
exit
postgresql.confsudo vi /var/lib/pgsql/14/data/postgresql.conf
找到并修改:
listen_addresses = '*'
pg_hba.confsudo vi /var/lib/pgsql/14/data/pg_hba.conf
在末尾添加:
# IPv4 remote
host all all 0.0.0.0/0 md5
sudo systemctl restart postgresql-14
sudo firewall-cmd --add-port=5432/tcp --permanent
sudo firewall-cmd --reload
psql -h 127.0.0.1 -U postgres -d postgres
| 功能 | 命令 |
|---|---|
| 查看状态 | systemctl status postgresql-14 |
| 重启 | systemctl restart postgresql-14 |
| 进入数据库 | sudo -u postgres psql |
| 查看数据库 | \l |
| 切换数据库 | \c dbname |
| PostgreSQL 版本 | 服务名 |
|---|---|
| 12 | postgresql-12 |
| 13 | postgresql-13 |
| 14 | postgresql-14 |
| 15 | postgresql-15 |
如果你需要:
可以直接告诉我你的 CentOS 版本 + PostgreSQL 版本 + 使用场景,我可以给你更精确的方案。