在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

相关推荐
777VG3 小时前
PostgreSQL +martin将多张表输出成一个 MVT
前端·数据库·postgresql
IvorySQL4 小时前
PG 日报|优化缓冲区批量扫描,降低多套接字并发竞争
数据库·人工智能·postgresql·开源·区块链
冰封之寂5 小时前
Docker 资源管理终极指南:CPU、内存与 IO 的精细化控制
linux·运维·服务器·docker·云计算
xiaoye-duck5 小时前
《Linux系统编程》Linux 系统多线程(五):<线程同步与互斥>线程互斥(上):从条件变量到生产者消费者模型详解
linux·线程
一叶龙洲7 小时前
向日葵远程Ubuntu,支持隐私屏
linux·运维·ubuntu
酷可达拉斯7 小时前
Linux操作系统-shell编程之Grep与正则表达式
linux·运维·服务器·正则表达式·centos
寒水馨8 小时前
Linux下载、安装llama.cpp-b10068(附安装包llama-b10068-bin-ubuntu-vulkan-x64.tar.gz)
linux·ubuntu·llm·llama·本地部署·llama.cpp·推理引擎
我星期八休息10 小时前
网络编程—UDP与TCP
linux·运维·网络·数据库·网络协议·tcp/ip·udp
ziguo112211 小时前
C/C++ 错误处理全解:从 errno 到 C++ 异常
linux·c语言·c++·windows·visual studio
三84412 小时前
使用Samba/NFS实现文件共享/自动挂载共享目录/autofs自动挂载服务
linux·服务器·网络