PostgreSQL(简称PG)是功能最强大的开源关系型数据库,以其数据完整性、扩展性和SQL标准兼容性著称。越来越多的Web应用和API后端选择PostgreSQL作为主数据库。本文介绍在搬瓦工VPS上安装和配置PostgreSQL的完整流程。
Tip: PostgreSQL内存占用比MySQL略高,建议VPS至少1GB内存。如果内存不足,请先配置Swap。
Ubuntu/Debian官方仓库包含PostgreSQL,但版本可能不是最新。推荐使用PostgreSQL官方源安装最新稳定版:
# 添加PostgreSQL官方APT源
apt install -y gnupg2
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
# 更新并安装
apt update
apt install postgresql postgresql-contrib -y
也可以直接使用系统自带版本(更简单):
apt update
apt install postgresql postgresql-contrib -y
安装后PostgreSQL自动启动,确认状态:
systemctl status postgresql
systemctl enable postgresql
CentOS系统建议通过官方仓库安装最新版本:
# 安装官方仓库(以PostgreSQL 16为例)
dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# 禁用系统自带模块
dnf -qy module disable postgresql
# 安装PostgreSQL 16
dnf install -y postgresql16-server postgresql16-contrib
# 初始化数据库
/usr/pgsql-16/bin/postgresql-16-setup initdb
# 启动并设置开机自启
systemctl start postgresql-16
systemctl enable postgresql-16
PostgreSQL安装后会创建一个名为postgres的系统用户和同名数据库超级用户。首先切换到该用户并设置密码:
# 切换到postgres用户
su - postgres
# 进入PostgreSQL命令行
psql
# 设置postgres用户密码
ALTER USER postgres PASSWORD 'YourStrongPassword';
# 退出psql
\q
PostgreSQL提供了便捷的命令行工具来创建用户和数据库:
# 以postgres用户身份操作
su - postgres
# 创建新用户(带密码和创建数据库权限)
createuser --interactive --pwprompt myapp_user
# 创建数据库并指定所有者
createdb -O myapp_user myapp_db -E UTF8
也可以在psql中使用SQL语句:
psql
-- 创建用户
CREATE USER myapp_user WITH PASSWORD 'SecurePass123!';
-- 创建数据库
CREATE DATABASE myapp_db OWNER myapp_user ENCODING 'UTF8';
-- 授予权限
GRANT ALL PRIVILEGES ON DATABASE myapp_db TO myapp_user;
-- 查看所有数据库
\l
-- 查看所有用户
\du
pg_hba.conf是PostgreSQL的客户端认证配置文件,控制谁可以从哪里以什么方式连接数据库:
# 查找配置文件位置
sudo -u postgres psql -c "SHOW hba_file;"
# 通常在 /etc/postgresql/16/main/pg_hba.conf (Ubuntu)
# 或 /var/lib/pgsql/16/data/pg_hba.conf (CentOS)
编辑pg_hba.conf,常见配置:
# 本地连接使用密码认证(将peer改为md5或scram-sha-256)
local all all scram-sha-256
# 允许本机IPv4连接
host all all 127.0.0.1/32 scram-sha-256
# 允许特定IP远程连接
host all all 192.168.1.0/24 scram-sha-256
# 允许所有IP连接(不推荐用于生产)
host all all 0.0.0.0/0 scram-sha-256
修改后重新加载配置:
systemctl reload postgresql
除了修改pg_hba.conf,还需要修改postgresql.conf中的监听地址:
# 编辑postgresql.conf
# Ubuntu: /etc/postgresql/16/main/postgresql.conf
# CentOS: /var/lib/pgsql/16/data/postgresql.conf
# 找到listen_addresses行,修改为:
listen_addresses = '*'
# 修改后重启PostgreSQL
systemctl restart postgresql
Tip: 开放远程访问后,务必通过防火墙限制5432端口的访问来源。
针对搬瓦工VPS的典型配置(1-2GB内存),建议调整以下参数:
# 编辑postgresql.conf
# 内存配置
shared_buffers = 256MB # 约为总内存的25%
effective_cache_size = 768MB # 约为总内存的75%
work_mem = 4MB
maintenance_work_mem = 64MB
# WAL配置
wal_buffers = 16MB
checkpoint_completion_target = 0.9
# 连接配置
max_connections = 50
# 日志配置
logging_collector = on
log_min_duration_statement = 1000 # 记录超过1秒的慢查询
修改后重启PostgreSQL使配置生效:
systemctl restart postgresql
PostgreSQL的交互式终端psql有许多实用的快捷命令:
\l -- 列出所有数据库
\c dbname -- 切换到指定数据库
\dt -- 列出当前数据库的表
\du -- 列出所有用户/角色
\d table -- 查看表结构
\di -- 列出索引
\ds -- 列出序列
\q -- 退出psql
\? -- 显示帮助
关于数据库的选择建议,可以参考MySQL vs PostgreSQL vs MariaDB选择指南。
Tip: 更多教程请查看新手教程。