pgsql下载安装(CentOS7,pgsql14)

pgsql下载安装

一、下载pgsql14

把默认的官方源替换为阿里云的 CentOS 7

1. 备份原有配置

ini 复制代码
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

2. 下载阿里云 CentOS 7 源文件

ini 复制代码
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

​ 如果 wget 也报错,用 curl(wget成功不需要这一步):

ini 复制代码
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

3. 清空并重建 yum 缓存

ini 复制代码
yum clean all
yum makecache

​ yum下载pgsql

ini 复制代码
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

yum-config-manager --disable pgdg12
yum-config-manager --disable pgdg13

sudo yum install -y postgresql14-server

sudo /usr/pgsql-14/bin/postgresql-14-setup initdb

sudo systemctl enable postgresql-14

sudo systemctl start postgresql-14

二、 修改 pg_hba.conf和postgresql.conf

ini 复制代码
vim  /var/lib/pgsql/14/data/postgresql.conf

​ 找到监听地址,去掉注释并修改为:

ini 复制代码
listen_addresses = '*'
ini 复制代码
sudo vi /var/lib/pgsql/14/data/pg_hba.conf

找到底部类似这样的配置:

ini 复制代码
host    all             all             127.0.0.1/32            ident

在最下面添加一行(允许所有 IP 密码连接):

ini 复制代码
host    all             all             0.0.0.0/0               md5(或者scram-sha-256)

保存退出。


三、重启 PostgreSQL 让配置生效

ini 复制代码
sudo systemctl restart postgresql-14

四、设置 postgres 用户密码(必须设置,否则无法登录)

ini 复制代码
# 切换到postgres用户
sudo su - postgres

# 进入数据库命令行
psql

然后执行修改密码命令:

ini 复制代码
ALTER USER postgres PASSWORD '123456';

退出数据库:

ini 复制代码
\q

退出 postgres 用户:

ini 复制代码
exit

五、开放防火墙 5432 端口(远程连接必备)

ini 复制代码
sudo firewall-cmd --add-port=5432/tcp --permanent
sudo firewall-cmd --reload

如果没装防火墙,可以跳过。


六、现在你可以:

远程工具连接(Navicat/DBeaver/PGAdmin)

  • 主机:你的服务器 IP
  • 端口:5432
  • 用户:postgres
  • 密码:123456

七、常用命令(收藏)

ini 复制代码
# 启动
sudo systemctl start postgresql-14
# 停止
sudo systemctl stop postgresql-14
# 重启
sudo systemctl restart postgresql-14
# 开机自启
sudo systemctl enable postgresql-14
# 查看状态
sudo systemctl status postgresql-14
sql 复制代码
-- 建表
CREATE DATABASE test01 WITH OWNER = postgres ENCODING = 'UTF8' LC_CTYPE = 'zh_CN.UTF-8' LC_COLLATE = 'zh_CN.UTF-8';
相关推荐
cpolar技术支持7 小时前
本地数据库客户端只能自己用?用 cpolar 安全访问 NAS 上的 PostgreSQL 管理台
postgresql·内网穿透·cpolar·nas·pgadmin
l1t20 小时前
DeepSeek总结的pg_clickhouse 与 chdb 更新:编码、嵌套与类型
数据库·clickhouse·postgresql
余槐i2 天前
EXPLAIN 显示走了索引查询仍慢:回表与选择性在 500 万行表上拉开 10 倍耗时
数据库·postgresql·性能优化·vacuum·explain
梦在远山后3 天前
PostgreSQL 锁与死锁:结合 DevMind 讲清楚
python·postgresql·agent
浪潮IT馆3 天前
Windows 10 安装 PostgreSQL 9.6.24 完整教程
数据库·windows·postgresql
岳麓丹枫0013 天前
PG 孤儿物理复制槽残留导致 pg_wal 膨胀 19G 故障分析
postgresql
haerapi3 天前
把 PostgreSQL 复制巡检做成可审计闭环:确定性采集、阈值判定与受控模型归纳
数据库·postgresql
如果'\'真能转义说4 天前
Docker Desktop | 本地化挂载 Postgresql
docker·postgresql·容器
IvorySQL4 天前
PostgreSQL 日报 |RLS 机密性绕过漏洞(9 月 27 日)
数据库·postgresql