linux装postgresql需先确认发行版、版本和远程访问需求;centos/rhel用pgdg仓库,ubuntu需同步修改postgresql.conf和pg_hba.conf,源码编译须用postgres用户并设好path与pgdata权限。

Linux 上装 PostgreSQL,别盲目抄命令,先看清楚你用的是哪个发行版、要装什么版本、是否需要远程访问——这三件事没对齐,后面全白忙。
CentOS/RHEL 7/8 安装 PostgreSQL 12–15 的标准流程
官方推荐用 pgdg 仓库,不是系统自带的 postgresql 包(太旧,常缺 pg_dump 或 pg_restore)。关键步骤顺序不能错:
- 先加仓库:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm(RHEL/CentOS 7);RHEL 8 改用dnf+https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm - 再装服务端:
sudo yum install -y postgresql15-server(把15换成你要的版本号) - 初始化必须用对应版本的脚本:
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb(路径里带版本号,错一个就报command not found) - 启动前确认端口没被占:
ss -tlnp | grep :5432,否则systemctl start postgresql-15会静默失败
Ubuntu/Debian 系统安装后 pg_hba.conf 不生效?
常见现象是:改了 pg_hba.conf 允许 host all all 0.0.0.0/0 md5,但连不上,psql -h 192.168.x.x 报 connection refused 或 no pg_hba.conf entry。根本原因不是配置写错,而是两个地方没同步改:
PyCharm 2026.2.0.1 Linux版提供 JetBrains 官方 2026.2.0.1 版本安装包,适合需要指定 PyCharm 版本进行 Python 项目开发、运行和调试的用户。
-
postgresql.conf里listen_addresses默认是'localhost',得改成'localhost,192.168.x.x'或'*'(注意引号和逗号) -
pg_hba.conf修改后必须 reload:sudo systemctl reload postgresql(不是restart,否则连接中断) - Ubuntu 下配置文件路径通常是
/etc/postgresql/*/main/,*是版本号,用ls /etc/postgresql/确认,别进错目录
源码编译安装时 initdb 失败:Permission denied 或 No such file
这是新手高频翻车点。不是命令输错了,而是用户权限和环境变量没理清:
- 必须用非 root 用户(如
postgres)执行initdb,root 直接跑会报initdb: cannot be run as root -
PGDATA目录必须为空且属主为当前用户:chown -R postgres:postgres /data/pg15-data,chmod 0700 /data/pg15-data - 确保
PATH包含刚编译的 bin 目录,比如export PATH=/usr/local/pgsql/bin:$PATH,否则initdb找不到或调用到系统旧版本 - 如果提示
could not find function "pg_stat_statements"类扩展错误,说明编译时漏了--with-perl或--with-python,得重来
安装完连不上 postgres 用户?密码不对或认证方式卡住
默认安装后,postgres 用户没有密码,且 peer 认证只允许本地 Unix socket 登录。想用密码登录,必须两步一起动:
- 切到
postgres系统用户:sudo -u postgres psql,然后在 psql 里设密码:ALTER USER postgres PASSWORD 'your_secure_pass'; - 修改
pg_hba.conf对应行,把peer改成md5(IPv4 local 连接那行),再systemctl reload postgresql - 验证:退出 psql,用
psql -U postgres -h 127.0.0.1 -d postgres测试,必须带-h 127.0.0.1强制走 TCP,否则还是 peer
最容易被忽略的是:不同安装方式(包管理 vs 源码)下,配置文件路径、二进制位置、服务名全部不同,systemctl list-units | grep postgres 和 ps aux | grep postgres 多看两眼,比硬背教程有用得多。










