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

相关推荐
专注API从业者2 分钟前
告别手工盯盘:用 Open‑Claw 搭建电商商品自动化监控与数据分析实战
运维·数据结构·数据挖掘·数据分析·自动化
Rebecca_aLi4 分钟前
码匠教育:从手工表格到脚本自动化,Python 改造工作流的思路
运维·python·自动化
Yan卌6 分钟前
探析全球虫害治理前沿 —— 美国 PestWorld 展会简述
大数据·运维·人工智能
小此方6 分钟前
Linux网络(二十二):深入解析IP协议:从地址寻址、头部结构到内核机制与TTL环路控制
linux·网络·tcp/ip
Ruiery8 分钟前
Linux 6.6内核内存管理深度解析(四):大页(HugeTLB / THP)— 为什么一个页表项要管 2MB
linux·运维·服务器
Lsetea11 分钟前
OpenSSL报certificate rejected:error 28与CA辅助信任属性排查
linux·https·ssl证书·openssl·证书链
沫璃染墨13 分钟前
从零入门计算机网络系列(五):UDP套接字编程实战——从核心API到客户端与服务器通信
linux·网络·c++·vscode·网络协议·udp
Margrop1 Margrop16 分钟前
别再挨个试清华、中科大、阿里源了!国家队下场的“CERNET 镜像聚合站”,凭什么让你的下载直接起飞?
linux·开源镜像·cernet
一条破秋裤23 分钟前
05_设备树自定义属性_写入读取验证
linux·网络·数据库
此生决int35 分钟前
Linux系统(07)——进程概念(下)
linux·运维·服务器