在腾讯云服务器上安装 PostgreSQL 是一个常见的需求,下面以 CentOS 7/8 和 Ubuntu 20.04/22.04 为例,详细介绍安装步骤。
✅ 一、准备工作
-
登录腾讯云服务器
使用 SSH 登录你的腾讯云 CVM 实例:ssh root@你的公网IP -
更新系统包(推荐)
- Ubuntu:
sudo apt update && sudo apt upgrade -y - CentOS:
sudo yum update -y
- Ubuntu:
✅ 二、安装 PostgreSQL
方法一:Ubuntu 安装 PostgreSQL
1. 添加 PostgreSQL 官方仓库(推荐最新版本)
# 安装必要的工具
sudo apt install wget ca-certificates -y
# 添加 PostgreSQL 官方 GPG 密钥
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# 添加仓库(以 PostgreSQL 15 为例,适用于 Ubuntu 20.04/22.04)
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
# 更新包列表
sudo apt update
2. 安装 PostgreSQL
# 安装 PostgreSQL 15(或其他版本如 14、16)
sudo apt install postgresql-15 postgresql-client-15 -y
3. 启动并设置开机自启
sudo systemctl start postgresql
sudo systemctl enable postgresql
方法二:CentOS/RHEL 安装 PostgreSQL
1. 添加 PostgreSQL 官方仓库
# 安装 yum 源(以 PostgreSQL 15 为例)
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
注意:如果你使用的是 CentOS 8 或 Stream,请选择对应的版本(如 EL-8)
2. 安装 PostgreSQL 服务器和客户端
sudo yum install -y postgresql15-server postgresql15
3. 初始化数据库并启动
# 初始化数据目录
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
# 启动服务并设置开机自启
sudo systemctl start postgresql-15
sudo systemctl enable postgresql-15
✅ 三、配置 PostgreSQL
1. 切换到 postgres 用户
PostgreSQL 安装后会创建一个系统用户 postgres。
sudo -i -u postgres
2. 登录 PostgreSQL shell
psql
3. 设置 postgres 用户密码(可选但推荐)
ALTER USER postgres PASSWORD 'your_password';
记得替换
'your_password'为强密码。
4. 退出 psql
q
5. 退出 postgres 用户
exit
✅ 四、允许远程连接(可选)
如果需要从外部连接 PostgreSQL(如本地 Navicat、DBeaver 等),需修改配置。
1. 修改 postgresql.conf
路径通常为:
- Ubuntu:
/etc/postgresql/15/main/postgresql.conf - CentOS:
/var/lib/pgsql/15/data/postgresql.conf
编辑文件:
sudo vim /path/to/postgresql.conf
修改以下行:
listen_addresses = 'localhost' # 改为:
listen_addresses = '*' # 或指定 IP,如 'your_server_ip'
2. 修改 pg_hba.conf(允许远程访问)
路径:
- Ubuntu:
/etc/postgresql/15/main/pg_hba.conf - CentOS:
/var/lib/pgsql/15/data/pg_hba.conf
在文件末尾添加一行(允许特定 IP 或所有 IP):
# 允许所有 IPv4 客户端使用密码登录
host all all 0.0.0.0/0 md5
⚠️ 注意:开放
0.0.0.0/0有安全风险,建议改为具体 IP,如123.123.123.123/32
3. 重启 PostgreSQL 服务
# Ubuntu
sudo systemctl restart postgresql
# CentOS
sudo systemctl restart postgresql-15
✅ 五、配置腾讯云安全组
- 登录 腾讯云控制台 → 进入 云服务器 CVM → 找到你的实例。
- 点击「安全组」→ 编辑入站规则。
- 添加规则:
- 协议类型:TCP
- 端口:
5432 - 源 IP:建议填写你的公网 IP(如
123.123.123.123/32),或临时0.0.0.0/0(测试用,不推荐长期使用)
✅ 六、测试远程连接
使用工具(如 pgAdmin、DBeaver、Navicat)连接:
- 主机:你的腾讯云公网 IP
- 端口:5432
- 用户名:
postgres - 密码:你设置的密码
✅ 七、常用命令汇总
| 功能 | 命令 |
|---|---|
| 启动服务 | sudo systemctl start postgresql |
| 停止服务 | sudo systemctl stop postgresql |
| 重启服务 | sudo systemctl restart postgresql |
| 查看状态 | sudo systemctl status postgresql |
| 切换用户 | sudo -i -u postgres |
| 进入 psql | psql |
✅ 小贴士
- 默认数据目录:
- Ubuntu:
/var/lib/postgresql/15/main - CentOS:
/var/lib/pgsql/15/data
- Ubuntu:
- 日志路径:通常在数据目录下的
log/或pg_log/文件夹中。
如果你有特定版本需求(如 PostgreSQL 14 或 16),或使用的是 TencentOS Server,也可以告诉我,我可以提供定制化指导。
是否需要我帮你写一个一键安装脚本?
CCLOUD博客