Oracle linux 7.9 安装PG 17.9

如果你需要特定版本的 PostgreSQL 或者需要编译自定义的扩展,你可以从源代码编译安装:

  1. 安装依赖

    复制代码

    bash

    sudo yum groupinstall "Development Tools" sudo yum install zlib-devel readline-devel openssl-devel libyaml-devel

  2. 下载源代码

    访问 PostgreSQL 官网 下载源代码。例如,下载 postgresql-14.0.tar.gz

    复制代码

    bash

    wget https://ftp.postgresql.org/pub/source/v14.0/postgresql-14.0.tar.gz tar -xzf postgresql-14.0.tar.gz cd postgresql-14.0

  3. 配置和编译

    复制代码

    bash

    ./configure --prefix=/usr/local/pgsql # 可以选择其他目录 make sudo make install

方法2:使用源代码编译安装

如果你需要特定版本的 PostgreSQL 或者需要编译自定义的扩展,你可以从源代码编译安装:

  1. 安装依赖

    复制代码

    bash

    sudo yum groupinstall "Development Tools" sudo yum install zlib-devel readline-devel openssl-devel libyaml-devel

  2. 下载源代码

    访问 PostgreSQL 官网 下载源代码。例如,下载 postgresql-14.0.tar.gz

    复制代码

    bash

    wget https://ftp.postgresql.org/pub/source/v14.0/postgresql-14.0.tar.gz tar -xzf postgresql-14.0.tar.gz cd postgresql-14.0

Index of /pub/source/v17.9/https://ftp.postgresql.org/pub/source/v17.9/

  1. 配置和编译

    复制代码

    bash

    ./configure --prefix=/usr/local/pgsql # 可以选择其他目录 make sudo make install

    checking for icu-uc icu-i18n... no
    configure: error: ICU library not found # 提示,未找到 ICU 库
    If you have ICU already installed, see config.log for details on the
    failure. It is possible the compiler isn't looking in the proper directory.
    Use --without-icu to disable ICU support.

安装libicu-devel包:

复制代码
yum install -y libicu-devel
  1. 初始化数据库并启动服务

    复制代码

    bash

    sudo /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data # 根据实际路径调整 echo "export PATH=/usr/local/pgsql/bin:$PATH" >> ~/.bashrc # 添加到 PATH 中 source ~/.bashrc pg_ctl -D /usr/local/pgsql/data -l logfile start # 启动服务,根据实际路径调整

  1. 初始化数据库并启动服务

    复制代码

    bash

    sudo /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data # 根据实际路径调整 echo "export PATH=/usr/local/pgsql/bin:$PATH" >> ~/.bashrc # 添加到 PATH 中 source ~/.bashrc pg_ctl -D /usr/local/pgsql/data -l logfile start # 启动服务,根据实际路径调整

2.3.3 创建数据库用户和组

PostgreSQL默认不支持 以root 身份启动服务,虽然也可修改源码实现root启动,但基于安全考虑不建议,因此必须创建一个用于启动PostgrepSQL的普通用户

复制代码
# 创建数据库用户和组,注意此用户需要可以交互登录
# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server、Ubuntu、Debian
useradd -s /bin/bash -m -d /home/postgres postgres

# openSUSE
groupadd postgres
useradd -s /bin/bash -m -d /home/postgres -g postgres postgres

# 修改postgres密码
echo postgres:123456|chpasswd

2.3.4 创建数据目录并授权

复制代码
mkdir -p /data/pgsql/
chown -R postgres:postgres /data/pgsql/

2.3.5 设置环境变量

复制代码
cat > /etc/profile.d/pgsql.sh <<EOF
export PGHOME=/apps/pgsql
export PATH=\$PGHOME/bin/:\$PATH
export PGDATA=/data/pgsql
export PGUSER=postgres
export MANPATH=\$PGHOME/share/man
EOF

. /etc/profile.d/pgsql.sh

2.3.6 初始化数据库

复制代码
[root@rocky10 postgresql-17.6]# su - postgres -c '/apps/pgsql/bin/initdb  -D /data/pgsql'
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /data/pgsql ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default "max_connections" ... 100
selecting default "shared_buffers" ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /data/pgsql -l logfile start

root@o19c postgresql-17.9# chown postgres /usr/local/pgsql/data

root@o19c postgresql-17.9# su - postgres

Last login: Wed Jul 1 05:07:09 EDT 2026 on pts/0

postgres@o19c \~$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

The files belonging to this database system will be owned by user "postgres".

This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".

The default database encoding has accordingly been set to "UTF8".

The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/pgsql/data ... ok

creating subdirectories ... ok

selecting dynamic shared memory implementation ... posix

selecting default "max_connections" ... 100

selecting default "shared_buffers" ... 128MB

selecting default time zone ... America/New_York

creating configuration files ... ok

running bootstrap script ... ok

performing post-bootstrap initialization ... ok

syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections

initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

postgres@o19c \~$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

waiting for server to start.... done

server started

2.3.7 准备启动脚本并启动服务

复制代码
# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server、openSUSE
cat > /usr/lib/systemd/system/postgresql.service <<EOF 
[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
User=postgres
Group=postgres
ExecStart=/apps/pgsql/bin/postgres -D /data/pgsql
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

# Ubuntu和Debian
cat > /lib/systemd/system/postgresql.service <<EOF 
[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
User=postgres
Group=postgres
ExecStart=/apps/pgsql/bin/postgres -D /data/pgsql
ExecReload=/bin/kill -HUP

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl enable --now postgresql

2.3.8 登录测试

复制代码
[root@rocky10 postgresql-17.6]# sudo -u postgres /apps/pgsql/bin/psql -c "SELECT version();"
                                                version                                                
-------------------------------------------------------------------------------------------------------
 PostgreSQL 17.6 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0, 64-bit
(1 row)

[root@rocky10 postgresql-17.6]# su - postgres
Last login: Mon Sep  8 16:47:39 CST 2025 on pts/0
[postgres@rocky10 ~]$ psql
psql (17.6)
Type "help" for help.

postgres=# help
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
postgres=# \q

PostgreSQL 默认情况下不允许远程登录,因为它只监听本地回环接口(localhost),即只允许来自同一台服务器的连接。如果你想允许远程客户端连接到你的 PostgreSQL 数据库服务器,你需要进行一些配置更改。以下是一些步骤来实现远程登录:

1. 修改 postgresql.conf 文件

你需要修改 PostgreSQL 的配置文件 postgresql.conf,以允许远程连接。

  1. 找到 listen_addresses 参数,并设置其值为 '*',这样 PostgreSQL 将监听所有可用的 IP 地址。

    复制代码

    ini

    listen_addresses = '*'

    或者,如果你只想监听特定的IP地址,可以指定该IP地址,例如:

    复制代码

    ini

    listen_addresses = '192.168.1.100'

  2. 确保 pg_hba.conf 文件已正确配置以允许远程连接。

2. 修改 pg_hba.conf 文件

pg_hba.conf 文件用于控制哪些客户端可以连接到数据库服务器,以及使用何种认证方式。

  1. 打开 pg_hba.conf 文件,通常位于 PostgreSQL 数据目录下。

  2. 添加或修改一条规则以允许从远程地址连接。例如,允许所有 IP 地址使用密码认证:

    复制代码

    ini

    host all all 0.0.0.0/0 md5
    或者,如果你只想允许特定的 IP 地址或子网:

    复制代码

    ini

    host all all 192.168.1.0/24 md5
    这里 md5 表示使用 MD5 密码加密方式进行认证。你也可以使用 passwordtrust 等其他认证方式,但出于安全考虑,推荐使用 md5

3. 重启 PostgreSQL 服务

修改配置文件后,需要重启 PostgreSQL 服务以使更改生效。

  • 在 Linux 上,可以使用以下命令:

postgres@o19c data$ pwd

/usr/local/pgsql/data

postgres@o19c data$

postgres@o19c data$ ls *conf

pg_hba.conf pg_ident.conf postgresql.auto.conf postgresql.conf

postgres@o19c data$ vi postgresql.conf

postgres@o19c data$ vi pg_hba.conf

postgres@o19c data$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile stop

waiting for server to shut down.... done

server stopped

postgres@o19c data$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

waiting for server to start.... done

server started

2.3.9 一键安装PostgreSQL源码编译的脚本

Shell脚本源码地址:

Gitee:https://gitee.com/raymond9/shell

Github:https://github.com/raymond999999/shell

可以去上面的Gitee或Github代码仓库拉取脚本。

复制代码
[root@rocky10 ~]# cat install_postgresql_source.sh
#!/bin/bash
#
#**********************************************************************************
#Author:        Raymond
#QQ:            88563128
#MP:            Raymond运维
#Date:          2025-09-22
#FileName:      install_postgresql_source.sh
#URL:           https://wx.zsxq.com/group/15555885545422
#Description:   The postgresql source script install supports 
#               "Rocky Linux 8, 9 and 10, AlmaLinux 8, 9 and 10, CentOS 7, 
#               CentOS Stream 8, 9 and 10, openEuler 22.03 and 24.03 LTS, 
#               AnolisOS 8 and 23, OpenCloudOS 8 and 9, Kylin Server v10 and v11, 
#               UOS Server v20, Ubuntu Server 18.04, 20.04, 22.04 and 24.04 LTS,  
#               Debian 11 , 12 and 13, openSUSE Leap 15" operating systems.
#Copyright (C): 2025 All rights reserved
#**********************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'

os(){
    . /etc/os-release
    MAIN_NAME=`sed -rn '/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`
    if [ ${MAIN_NAME} == "Kylin" ];then
        MAIN_VERSION_ID=`sed -rn '/^VERSION_ID=/s@.*="([[:alpha:]]+)(.*)"$@\2@p' /etc/os-release`
    else
        MAIN_VERSION_ID=`sed -rn '/^VERSION_ID=/s@.*="?([0-9]+)\.?.*"?@\1@p' /etc/os-release`
    fi
    if [ ${MAIN_NAME} == "Ubuntu" -o ${MAIN_NAME} == "Debian" ];then
        FULL_NAME="${PRETTY_NAME}"
    elif [ ${MAIN_NAME} == "UOS" ];then
        FULL_NAME="${NAME}"
    else
        FULL_NAME="${NAME} ${VERSION_ID}"
    fi
}

os
SRC_DIR=/usr/local/src
INSTALL_DIR=/apps/pgsql
DATA_DIR=/data/pgsql
DB_USER=postgres
POSTGRESQL_VERSION=17.6
POSTGRESQL_URL="https://ftp.postgresql.org/pub/source/v${POSTGRESQL_VERSION}/"
POSTGRESQL_FILE="postgresql-${POSTGRESQL_VERSION}.tar.gz"
DB_USER_PASSWORD=123456

check_file(){
    cd  ${SRC_DIR}
    if [ ${MAIN_NAME} == "Rocky" -o ${MAIN_NAME} == "AlmaLinux" -o ${MAIN_NAME} == "CentOS" -o ${MAIN_NAME} == "Anolis" -o ${MAIN_NAME} == "OpenCloudOS" -o ${MAIN_NAME} == "Kylin" ];then
        rpm -q wget &> /dev/null || { ${COLOR}"安装wget工具,请稍等......"${END};yum -y install wget &> /dev/null; }
    fi
    if [ ! -e ${POSTGRESQL_FILE} ];then
        ${COLOR}"缺少${POSTGRESQL_FILE}文件!"${END}
        ${COLOR}'开始下载PostgreSQL源码包......'${END}
        wget ${POSTGRESQL_URL}${POSTGRESQL_FILE} || { ${COLOR}"PostgreSQL源码包下载失败!"${END}; exit; }
    else
        ${COLOR}"${POSTGRESQL_FILE}文件已准备好!"${END}
    fi
}

install_postgresql(){
    [ -d ${INSTALL_DIR} ] && { ${COLOR}"PostgreSQL数据库已存在,安装失败!"${END};exit; }
    ${COLOR}"开始安装PostgreSQL数据库......"${END}
    ${COLOR}'开始安装PostgreSQL依赖包,请稍等......'${END}
    if [ ${MAIN_NAME} == "Rocky" -o ${MAIN_NAME} == "AlmaLinux" -o ${MAIN_NAME} == "CentOS" -o ${MAIN_NAME} == "OpenCloudOS" ];then
        yum install -y gcc libicu-devel bison flex perl readline-devel zlib-devel openssl-devel libxml2-devel systemd-devel docbook-dtds docbook-style-xsl libxslt &> /dev/null
    fi
    if [ ${MAIN_NAME} == "openEuler" ];then
        yum install -y gcc libicu-devel bison flex readline-devel zlib-devel openssl-devel libxml2-devel systemd-devel docbook-dtds docbook-style-xsl libxslt &> /dev/null
    fi
    if [ ${MAIN_NAME} == "Anolis" ];then
        if [ ${MAIN_VERSION_ID} == 8 ];then
            yum install -y gcc libicu-devel bison flex readline-devel zlib-devel openssl-devel libxml2-devel systemd-devel make docbook-dtds docbook-style-xsl libxslt &> /dev/null
        else
            yum install -y gcc libicu-devel bison flex readline-devel zlib-devel openssl-devel libxml2-devel systemd-devel perl-FindBin perl-core docbook-dtds docbook-style-xsl libxslt &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "Kylin" ];then
        yum install -y gcc libicu-devel bison flex perl readline-devel zlib-devel openssl-devel libxml2-devel systemd-devel docbook-dtds docbook-style-xsl libxslt &> /dev/null
    fi
    if [ ${MAIN_NAME} == "UOS" ];then
        if [ ${MAIN_VERSION_ID} == 20 ];then
            yum install -y libicu-devel bison flex perl readline-devel systemd-devel docbook-dtds docbook-style-xsl libxslt &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "openSUSE" ];then
        if [ ${MAIN_VERSION_ID} == 15 ];then
            zypper install -y gcc libicu-devel bison flex readline-devel zlib-devel openssl-devel libxml2-devel systemd-devel make docbook-xsl-stylesheets &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "Ubuntu" ];then
        if [ ${MAIN_VERSION_ID} == 18 ];then
            apt update && apt install -y gcc pkg-config libicu-dev bison flex libreadline-dev libssl-dev libxml2-dev libsystemd-dev make docbook-xml docbook-xsl libxml2-utils xsltproc fop
        else
            apt update && apt install -y gcc pkg-config libicu-dev bison flex libreadline-dev zlib1g-dev libssl-dev libxml2-dev libsystemd-dev make docbook-xml docbook-xsl libxml2-utils xsltproc fop
        fi
    fi
    if [ ${MAIN_NAME} == 'Debian' ];then
        apt update && apt install -y gcc pkg-config libicu-dev bison flex libreadline-dev zlib1g-dev libssl-dev libxml2-dev libsystemd-dev make docbook-xml docbook-xsl libxml2-utils xsltproc fop
    fi
    ${COLOR}'开始编译安装PostgreSQL,请稍等......'${END}
    cd  ${SRC_DIR}
    if [ ${MAIN_NAME} == "openEuler" ];then
        if [ ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "Anolis" ];then
        if [ ${MAIN_VERSION_ID} == 23 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    if [ ${MAIN_NAME} == "OpenCloudOS" ];then
        if [ ${MAIN_VERSION_ID} == 9 ];then
            yum install -y tar &> /dev/null
        fi
    fi
    tar xf ${POSTGRESQL_FILE}
    POSTGRESQL_DIR=`echo ${POSTGRESQL_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${POSTGRESQL_DIR}
    ./configure --prefix=${INSTALL_DIR} --with-openssl --with-libxml --with-systemd
    make -j $(nproc) world
    make install-world
    [ $? -eq 0 ] && ${COLOR}"PostgreSQL编译安装成功!"${END} ||  { ${COLOR}"PostgreSQL编译安装失败,退出!"${END};exit; }
    if [ ${MAIN_NAME} == "openSUSE" ];then
        id ${DB_USER} &> /dev/null || { groupadd ${DB_USER} && useradd -s /bin/bash -m -d /home/${DB_USER} -g ${DB_USER} ${DB_USER}; ${COLOR}"成功创建${DB_USER}用户!"${END}; }
    else
        id ${DB_USER} &> /dev/null || { useradd -s /bin/bash -m -d /home/${DB_USER} ${DB_USER} ; ${COLOR}"成功创建${DB_USER}用户!"${END}; }
    fi
    echo ${DB_USER}:${DB_USER_PASSWORD}|chpasswd
    [ -d ${DATA_DIR} ] || mkdir -p ${DATA_DIR}/
    chown -R ${DB_USER}:${DB_USER} ${DATA_DIR}/   
    cat > /etc/profile.d/pgsql.sh <<EOF
export PGHOME=${INSTALL_DIR}
export PATH=${INSTALL_DIR}/bin/:\$PATH
export PGDATA=${DATA_DIR}
export PGUSER=${DB_USER}
export MANPATH=${INSTALL_DIR}/share/man

alias pgstart="pg_ctl -D ${DATA_DIR} start"
alias pgstop="pg_ctl -D ${DATA_DIR} stop"
alias pgrestart="pg_ctl -D ${DATA_DIR} restart"
alias pgstatus="pg_ctl -D ${DATA_DIR} status"
EOF
    su - ${DB_USER} -c "${INSTALL_DIR}/bin/initdb -D ${DATA_DIR}"
    if [ ${MAIN_NAME} == "Ubuntu" -o ${MAIN_NAME} == "Debian" ];then
        cat > /lib/systemd/system/postgresql.service <<EOF
[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
User=${DB_USER}
Group=${DB_USER}
ExecStart=${INSTALL_DIR}/bin/postgres -D ${DATA_DIR}
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF
    else
        cat > /usr/lib/systemd/system/postgresql.service <<EOF
[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
User=${DB_USER}
Group=${DB_USER}
ExecStart=${INSTALL_DIR}/bin/postgres -D ${DATA_DIR}
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF
    fi
    systemctl daemon-reload && systemctl enable --now postgresql &> /dev/null
    [ $? -ne 0 ] && { ${COLOR}"数据库启动失败,退出!"${END};exit; }
    ${COLOR}"${FULL_NAME}操作系统,PostgreSQL数据库安装完成!"${END}
}

main(){
    check_file
    install_postgresql
}

if [ ${MAIN_NAME} == "Rocky" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "AlmaLinux" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "CentOS" ];then
    if [ ${MAIN_VERSION_ID} == 7 -o ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 -o ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "openEuler" ];then
    if [ ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Anolis" ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 23 ];then
        main
    fi
elif [ ${MAIN_NAME} == 'OpenCloudOS' ];then
    if [ ${MAIN_VERSION_ID} == 8 -o ${MAIN_VERSION_ID} == 9 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Kylin" ];then
    if [ ${MAIN_VERSION_ID} == 10 ];then
        main
    fi
elif [ ${MAIN_NAME} == "UOS" ];then
    if [ ${MAIN_VERSION_ID} == 20 ];then
        main
    fi
elif [ ${MAIN_NAME} == "openSUSE" ];then
    if [ ${MAIN_VERSION_ID} == 15 ];then
        main
    fi
elif [ ${MAIN_NAME} == "Ubuntu" ];then
    if [ ${MAIN_VERSION_ID} == 18 -o ${MAIN_VERSION_ID} == 20 -o ${MAIN_VERSION_ID} == 22 -o ${MAIN_VERSION_ID} == 24 ];then
        main
    fi
elif [ ${MAIN_NAME} == 'Debian' ];then
    if [ ${MAIN_VERSION_ID} == 11 -o ${MAIN_VERSION_ID} == 12 -o ${MAIN_VERSION_ID} == 13 ];then
        main
    fi
else
    ${COLOR}"此脚本不支持${FULL_NAME}操作系统!"${END}
fi