别再用老方法了!在RHEL8上部署PostgreSQL 16,我推荐你用YUM源安装(附阿里云镜像配置) 在RHEL8上高效部署PostgreSQL 16YUM源安装全攻略当我们需要在生产环境或学习环境中快速部署PostgreSQL 16时选择正确的安装方式至关重要。面对源码编译、RPM包安装和YUM在线安装三种主流方式每种方法都有其适用场景和优缺点。本文将深入分析这三种安装方式的差异并重点推荐最适合大多数用户的YUM源安装方案特别是针对国内用户访问官方源速度慢的问题提供阿里云镜像配置的完整解决方案。1. 安装方式对比与选择在RHEL8系统上部署PostgreSQL 16我们主要有三种安装方式可选源码编译安装优点完全自定义编译选项灵活性最高缺点过程复杂依赖管理困难耗时较长适用场景需要特定编译选项或有特殊定制需求的高级用户RPM包安装优点比源码安装简单无需编译缺点依赖关系处理麻烦容易出现依赖地狱适用场景离线环境或需要精确控制软件版本的情况YUM源安装优点自动解决依赖关系安装简单快捷便于后续升级缺点需要网络连接默认源在国内可能速度较慢适用场景大多数生产环境和学习环境特别是需要快速部署的情况对于大多数用户尤其是新手或时间有限的运维人员YUM源安装是最推荐的方式。它不仅简化了安装过程还自动处理了复杂的依赖关系大大降低了出错概率。2. 系统准备与环境配置在开始安装PostgreSQL 16之前我们需要对RHEL8系统进行一些基础配置2.1 创建专用用户和目录# 创建postgres用户组和用户 sudo groupadd -g 60000 postgres sudo useradd -u 60000 -g postgres postgres sudo passwd postgres # 创建PostgreSQL相关目录 sudo mkdir -p /pgdata/{data,archive,backup,scripts} sudo chown -R postgres:postgres /pgdata sudo chmod -R 750 /pgdata2.2 系统参数优化为了PostgreSQL能够获得最佳性能我们需要调整一些系统参数# 关闭SELinux生产环境请谨慎评估 sudo setenforce 0 sudo sed -i s/SELINUXenforcing/SELINUXdisabled/g /etc/selinux/config # 调整系统资源限制 sudo tee -a /etc/security/limits.conf EOF postgres soft nofile 65536 postgres hard nofile 65536 postgres soft nproc 65536 postgres hard nproc 65536 EOF2.3 安装基础依赖包sudo yum install -y openssl openssl-devel pam pam-devel libxml2 libxml2-devel \ libxslt libxslt-devel perl perl-devel python3-devel perl-ExtUtils-Embed \ readline readline-devel zlib zlib-devel bzip2 bzip2-devel \ gettext gettext-devel flex gcc gcc-c libicu-devel3. 配置阿里云PostgreSQL YUM源由于PostgreSQL官方YUM源在国内访问速度较慢我们可以使用阿里云镜像源来加速下载3.1 安装PostgreSQL官方仓库配置包sudo dnf install -y https://mirrors.aliyun.com/postgresql/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm3.2 替换为阿里云镜像源编辑PostgreSQL的YUM源配置文件sudo sed -i s|https://download.postgresql.org/pub|https://mirrors.aliyun.com/postgresql|g /etc/yum.repos.d/pgdg-redhat-all.repo sudo sed -i s|https://download.postgresql.org/pub|https://mirrors.aliyun.com/postgresql|g /etc/yum.repos.d/pgdg-redhat.repo3.3 更新YUM缓存sudo yum clean all sudo yum makecache4. 使用YUM安装PostgreSQL 164.1 安装PostgreSQL服务器sudo yum install -y postgresql16-server postgresql16-contrib4.2 初始化数据库sudo /usr/pgsql-16/bin/postgresql-16-setup initdb4.3 配置环境变量为了方便使用PostgreSQL命令我们为postgres用户配置环境变量sudo tee -a ~postgres/.bash_profile EOF export PGDATA/var/lib/pgsql/16/data export PGHOME/usr/pgsql-16 export PATH\$PGHOME/bin:\$PATH export PGUSERpostgres export PGDATABASEpostgres EOF5. PostgreSQL基础配置5.1 配置postgresql.confsudo tee -a /var/lib/pgsql/16/data/postgresql.conf EOF listen_addresses * port 5432 max_connections 100 shared_buffers 128MB dynamic_shared_memory_type posix log_timezone Asia/Shanghai timezone Asia/Shanghai datestyle iso, mdy lc_messages en_US.UTF-8 lc_monetary en_US.UTF-8 lc_numeric en_US.UTF-8 lc_time en_US.UTF-8 default_text_search_config pg_catalog.english EOF5.2 配置pg_hba.confsudo tee /var/lib/pgsql/16/data/pg_hba.conf EOF # TYPE DATABASE USER ADDRESS METHOD local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust host all all 0.0.0.0/0 md5 EOF5.3 启动PostgreSQL服务sudo systemctl enable postgresql-16 sudo systemctl start postgresql-16 sudo systemctl status postgresql-166. 验证安装与基本操作6.1 连接数据库sudo -u postgres psql6.2 执行基本SQL命令-- 创建测试数据库 CREATE DATABASE testdb; -- 创建用户并授权 CREATE USER testuser WITH PASSWORD securepassword; GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser; -- 查看数据库列表 \l -- 查看用户列表 \du6.3 远程连接测试从另一台机器测试连接psql -h 服务器IP -U testuser -d testdb -W7. 常见问题解决7.1 连接被拒绝如果遇到连接被拒绝的问题请检查postgresql.conf中的listen_addresses是否设置为*pg_hba.conf中是否配置了相应的访问规则防火墙是否放行了5432端口7.2 性能调优建议对于生产环境建议根据服务器配置调整以下参数shared_buffers通常设置为物理内存的25%work_mem为每个操作分配的内存通常4-32MBmaintenance_work_mem维护操作使用的内存通常64-512MBeffective_cache_size通常设置为物理内存的50-75%7.3 备份与恢复设置定期备份策略# 基本备份命令 pg_dump -U postgres -d testdb -f /pgdata/backup/testdb_backup.sql # 恢复数据库 psql -U postgres -d testdb -f /pgdata/backup/testdb_backup.sql8. 后续维护与管理8.1 升级PostgreSQL使用YUM升级PostgreSQL非常简单sudo yum update postgresql16-server sudo systemctl restart postgresql-168.2 监控与日志PostgreSQL的日志默认位于/var/lib/pgsql/16/data/pg_log/可以使用以下命令查看实时日志sudo journalctl -u postgresql-16 -f8.3 扩展安装使用YUM安装常用扩展sudo yum install postgresql16-plperl postgresql16-plpython3