你的系统:Rocky Linux 8(GreenObsidian) psql 是postgresql的命令行客户端工具,分两种场景:只装客户端(只用来连远程pg)、完整安装pg服务端(本地跑数据库)

场景A:只装psql客户端(本机不跑pg数据库,只连接远程PG,工作最常用)

方式1:系统自带module流(最简单,不用额外加源)

# Rocky8 dnf模块,只安装client,不安装server服务
dnf install -y postgresql:12/client

安装完验证

psql --version

连接远程数据库示例

psql -h 192.168.1.100 -p 5432 -U postgres -d mydb

方式2:安装官方PGDG源,安装指定版本客户端(比如pg14/15,版本可控)

#1 安装pg官方yum源
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

#2 禁用系统自带postgresql模块,避免版本冲突
dnf module disable postgresql -y

#3 只装pg15客户端(不带server)
dnf install -y postgresql15

⚠️注意:postgresql15包只有客户端工具psql,不会安装postgresql‑server,不会启动数据库服务。 如果写postgresql15‑server才会安装数据库服务端。


场景B:完整安装PostgreSQL服务端(本机运行pg数据库,从零完整部署)Rocky8

#1 安装官方pgdg源
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

#2 关闭系统内置postgresql模块
dnf module disable postgresql -y

#3 安装服务端+客户端(pg15举例)
dnf install -y postgresql15-server postgresql15-contrib

#4 【非常重要】初始化数据库(首次安装必须执行!)
/usr/pgsql-15/bin/postgresql-15-setup initdb

#5 设置开机自启、启动服务
systemctl enable --now postgresql-15

#6 查看状态
systemctl status postgresql-15

初始配置

  1. 默认操作系统用户postgres,数据库超级用户postgres
#切换postgres用户登录本地库
su - postgres
psql
  1. 修改postgres数据库密码
ALTER USER postgres WITH PASSWORD '你的密码';
  1. 开放远程访问:修改/var/lib/pgsql/15/data/postgresql.conf
listen_addresses = '*'

修改pg_hba.conf,增加允许网段

host  all  all 0.0.0.0/0 scram-sha-256

重启服务:systemctl restart postgresql‑15

psql常用连接命令总结

#连接远程pg
psql -h ip地址 -p 端口 -U用户名 -d数据库名

#本地登录(操作系统身份认证)
su - postgres
psql

常见踩坑

  1. Rocky8直接dnf install postgresql会装module默认版本,容易版本混乱,生产建议用pgdg官方源。
  2. 装server之后,必须执行initdb初始化,否则服务启动失败!
  3. 只需要连别人的pg库,不要装xxx‑server包,只装客户端包即可
  4. psql客户端版本可以高于/低于服务端,大版本跨度过大容易出现兼容性警告。

区分包名记忆

  • postgresql15仅客户端,psql工具,不启动数据库服务
  • postgresql15‑server:完整数据库服务端,需要initdb初始化

如果你需要,我可以给你一份psql高频命令速查表。

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐