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高频命令速查表。

相关推荐
未济2 天前
linux 配置环境变量
linux
傲世仙尊2 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
_艾伦 耶格尔.2 天前
进程间通信
linux
AI职业加油站2 天前
AI智能体应用工程师证书:政策红利下的职业新风口
大数据·运维·人工智能·学习·职场发展
Liuqy-052 天前
Linux IO编程——静态库、动态库
linux
geovindu2 天前
sql: Transaction & Concurrency Patterns using postgresql 18
postgresql·数据库开发·数据库架构
此冬歌咏2 天前
K8s 节点故障实战:优雅驱逐 31 秒,硬故障 331 秒,以及那个永远 Pending 的 Pod
运维·k8s
彧azz2 天前
Linux 环境下 Redis 学习总结:数据类型、持久化、锁、事务、主从与缓存问题
linux·redis·笔记·学习·面试
-梅2 天前
linux(8) 软硬链接
linux·运维·服务器