在CentOS上以源码编译的方式安装PostgreSQL

下载目录:PostgreSQL: File Browser,我使用的PostgreSQLv17.5 。Linux系统:CentOS Linux release 7.9.2009 (Core)

  1. 安装依赖包和工具链(必须且重要!)

    shell 复制代码
    yum groupinstall "Development Tools" -y
    yum install -y readline-devel zlib-devel libicu-devel openssl-devel pam-devel libxml2-devel libxslt-devel systemd-devel
  2. 上传安装包至服务器后解压此包

    shell 复制代码
    # 先在Windows的CMD 或 macOS的terminal下执行scp命令
    scp postgresql-17.5.tar.gz root@192.168.31.199:/root
    # Linux执行以下命令
    tar -xvf postgresql-17.5.tar.gz # 解压
  3. 编译并安装

    shell 复制代码
    cd postgresql-17.5
    ./configure \
      --prefix=/opt/pgsql \ # 申明安装在哪里
      --with-openssl \
      --with-libxml \
      --with-icu \
      --with-systemd
    make -j $(nproc) && sudo make install # $(nproc)使用CPU多核编译
  4. 增加postgres组和postgres用户

    shell 复制代码
    groupadd postgres && useradd -g postgres postgres
    passwd postgres # 定义postgres用户密码
  5. 创建目录并授权:创建存储数据的文件夹(我创建在**/opt/pgsql/data**)

    shell 复制代码
    mkdir -p /opt/pgsql/data && chown -R postgres:postgres /opt/pgsql
  6. 初始化数据库

    shell 复制代码
    # 切换至postgres用户
    su -- postgres
    # 进入pgsql的bin目录
    cd /opt/pgsql/bin
    # 初始化数据库
    ./initdb -D /opt/pgsql/data --locale=en_US.UTF-8 --encoding=UTF8
  7. 修改配置文件

    • /opt/pgsql/data/postgresql.conf 新增内容如下

      tex 复制代码
      listen_addresses = '*' # 允许所有IP连接,默认是仅允许本地连接,此项必须改
      port = 5432       # 默认端口,此项可忽略不增
    • /opt/pgsql/data/ pg_hba.conf 修改内容如下

      tex 复制代码
      # IPv4 local connections:
      # host    all             all             127.0.0.1/32            trust # 注释掉原来的
      host    all             all             0.0.0.0/0            scram-sha-256 # 新增此行,允许所有IP通过密码连接
      # IPv6 local connections:
      # host    all             all             ::1/128                 trust # 注释掉原来的
      host    all             all             ::1/128                 scram-sha-256 # 新增此行,允许所有IP通过密码连接
  8. 新建/etc/systemd/system/postgresql.service服务文件,需用到root用户,请切换到root用户

    shell 复制代码
    su - root
    touch /etc/systemd/system/postgresql.service

    文件内容如下

    tex 复制代码
    [Unit]
    Description=PostgreSQL database server
    After=network.target
    
    [Service]
    User=postgres
    Group=postgres
    Type=notify
    User=postgres
    ExecStart=/opt/pgsql/bin/postgres -D /opt/pgsql/data
    ExecReload=/bin/kill -HUP $MAINPID
    
    [Install]
    WantedBy=multi-user.target

    修改完成后执行

    shell 复制代码
    chown postgres:postgres /etc/systemd/system/postgresql.service
    systemctl daemon-reload
    systemctl start postgresql
    systemctl enable postgresql # 不希望pgsql每次开机自启,请勿执行此行命令
  9. 修改postgresql数据超级用户------postgres的密码,并将postgres用户添加到路径,请先将Linux切换到postgres用户

    shell 复制代码
    su - postgres
    . /opt/pgsql/bin/psql -c "alter user postgres with password '自定义密码';"
    # 添加路径
    echo 'export PATH=$PATH:/opt/pgsql/bin' >> ~/.bash_profile
    echo 'export PGDATA=/opt/pgsql/data' >> ~/.bash_profile
    source ~/.bash_profile
  10. 开启防火墙端口(请先将Linux切换到root用户)

    shell 复制代码
    su - root
    firewall-cmd --add-port=5432/tcp --permanent # 开放端口且永久生效
    firewall-cmd --reload # 重载配置

若远程连接不上,那么请重启服务,用postgres用户或root都行systemctl restart postgresql

相关推荐
IvorySQL2 小时前
PostgreSQL 技术日报 (3月4日)|硬核干货 + 内核暗流一网打尽
数据库·postgresql·开源
0xDevNull5 小时前
Linux切换JDK版本详细教程
linux
进击的丸子5 小时前
虹软人脸服务器版SDK(Linux/ARM Pro)多线程调用及性能优化
linux·数据库·后端
IvorySQL1 天前
双星闪耀温哥华:IvorySQL 社区两项议题入选 PGConf.dev 2026
数据库·postgresql·开源
Johny_Zhao2 天前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw
赵渝强老师2 天前
【赵渝强老师】PostgreSQL中表的碎片
数据库·postgresql
chlk1233 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑3 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件3 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
xy123063 天前
OpenStack Train 部署实战(三)控制节点--keystone服务
centos·openstack