Rocky Linux 8 从零安装 psql(PostgreSQL 客户端)

你的系统: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

  2. 修改postgres数据库密码

    ALTER USER postgres WITH PASSWORD '你的密码';

  3. 开放远程访问:修改/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高频命令速查表。

相关推荐
Fu2067214 小时前
第一阶段项目
linux·服务器·网络
吴建旭 智宅焕5 小时前
智能家居全国交付的确定性架构设计与实现:从不确定转化到交付基础设施
运维·智能家居·devops
女神下凡11 小时前
芯参谋(119):凌阳(Sunplus)_开发指南
linux·arm开发·stm32·单片机·嵌入式硬件
可涵不会debug12 小时前
dufs轻量文件服务器教程:中文界面、WebDAV、上传分享与远程访问
运维·服务器
数据库急诊室_阿哲12 小时前
一周慢查询日志审计:1200 条里有多少真问题
运维·数据库·dba
zhangjw3413 小时前
2-4 集合运算 union /union all /intersect/except
数据库·postgresql
新*13 小时前
ES 7.17 三节点 Docker 集群的自动备份:从 NFS 被拦住到用 ACL 根治权限
运维·elasticsearch·docker·slm·快照备份
Decay__13 小时前
PostgreSQL DBA 日常运维手册 —— 会话、锁阻塞、慢查询、膨胀与每日巡检
运维·数据库·sql·postgresql
落羽的落羽14 小时前
【C++】现代容器: tuple、optional、variant、any、string_view
linux·开发语言·数据结构·c++·人工智能·计算机网络·算法
木子欢儿14 小时前
Ubuntu 26.04 LTS 安装Python 3.12
linux·运维·服务器·python·ubuntu