如果你需要特定版本的 PostgreSQL 或者需要编译自定义的扩展你可以从源代码编译安装安装依赖bashsudo yum groupinstall Development Tools sudo yum install zlib-devel readline-devel openssl-devel libyaml-devel下载源代码访问 PostgreSQL 官网 下载源代码。例如下载postgresql-14.0.tar.gzbashwget https://ftp.postgresql.org/pub/source/v14.0/postgresql-14.0.tar.gz tar -xzf postgresql-14.0.tar.gz cd postgresql-14.0配置和编译bash./configure --prefix/usr/local/pgsql # 可以选择其他目录 make sudo make install方法2使用源代码编译安装如果你需要特定版本的 PostgreSQL 或者需要编译自定义的扩展你可以从源代码编译安装安装依赖bashsudo yum groupinstall Development Tools sudo yum install zlib-devel readline-devel openssl-devel libyaml-devel下载源代码访问 PostgreSQL 官网 下载源代码。例如下载postgresql-14.0.tar.gzbashwget https://ftp.postgresql.org/pub/source/v14.0/postgresql-14.0.tar.gz tar -xzf postgresql-14.0.tar.gz cd postgresql-14.0Index of /pub/source/v17.9/https://ftp.postgresql.org/pub/source/v17.9/配置和编译bash./configure --prefix/usr/local/pgsql # 可以选择其他目录 make sudo make installchecking 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 isnt looking in the proper directory. Use --without-icu to disable ICU support.安装libicu-devel包yum install -y libicu-devel初始化数据库并启动服务bashsudo /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 # 启动服务根据实际路径调整初始化数据库并启动服务bashsudo /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|chpasswd2.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 PGUSERpostgres export MANPATH\$PGHOME/share/man EOF . /etc/profile.d/pgsql.sh2.3.6 初始化数据库[rootrocky10 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[rooto19c postgresql-17.9]# chown postgres /usr/local/pgsql/data[rooto19c postgresql-17.9]# su - postgresLast login: Wed Jul 1 05:07:09 EDT 2026 on pts/0[postgreso19c ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/dataThe 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 ... okcreating subdirectories ... okselecting dynamic shared memory implementation ... posixselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting default time zone ... America/New_Yorkcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okinitdb: warning: enabling trust authentication for local connectionsinitdb: 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[postgreso19c ~]$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile startwaiting for server to start.... doneserver started2.3.7 准备启动脚本并启动服务# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server、openSUSE cat /usr/lib/systemd/system/postgresql.service EOF [Unit] DescriptionPostgreSQL database server Afternetwork.target [Service] Userpostgres Grouppostgres ExecStart/apps/pgsql/bin/postgres -D /data/pgsql ExecReload/bin/kill -HUP \$MAINPID [Install] WantedBymulti-user.target EOF # Ubuntu和Debian cat /lib/systemd/system/postgresql.service EOF [Unit] DescriptionPostgreSQL database server Afternetwork.target [Service] Userpostgres Grouppostgres ExecStart/apps/pgsql/bin/postgres -D /data/pgsql ExecReload/bin/kill -HUP [Install] WantedBymulti-user.target EOF systemctl daemon-reload systemctl enable --now postgresql2.3.8 登录测试[rootrocky10 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) [rootrocky10 postgresql-17.6]# su - postgres Last login: Mon Sep 8 16:47:39 CST 2025 on pts/0 [postgresrocky10 ~]$ 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# \qPostgreSQL 默认情况下不允许远程登录因为它只监听本地回环接口localhost即只允许来自同一台服务器的连接。如果你想允许远程客户端连接到你的 PostgreSQL 数据库服务器你需要进行一些配置更改。以下是一些步骤来实现远程登录1. 修改postgresql.conf文件你需要修改 PostgreSQL 的配置文件postgresql.conf以允许远程连接。找到listen_addresses参数并设置其值为*这样 PostgreSQL 将监听所有可用的 IP 地址。inilisten_addresses *或者如果你只想监听特定的IP地址可以指定该IP地址例如inilisten_addresses 192.168.1.100确保pg_hba.conf文件已正确配置以允许远程连接。2. 修改pg_hba.conf文件pg_hba.conf文件用于控制哪些客户端可以连接到数据库服务器以及使用何种认证方式。打开pg_hba.conf文件通常位于 PostgreSQL 数据目录下。添加或修改一条规则以允许从远程地址连接。例如允许所有 IP 地址使用密码认证inihost all all 0.0.0.0/0 md5或者如果你只想允许特定的 IP 地址或子网inihost all all 192.168.1.0/24 md5这里md5表示使用 MD5 密码加密方式进行认证。你也可以使用password、trust等其他认证方式但出于安全考虑推荐使用md5。3. 重启 PostgreSQL 服务修改配置文件后需要重启 PostgreSQL 服务以使更改生效。在 Linux 上可以使用以下命令[postgreso19c data]$ pwd/usr/local/pgsql/data[postgreso19c data]$[postgreso19c data]$ ls *confpg_hba.conf pg_ident.conf postgresql.auto.conf postgresql.conf[postgreso19c data]$ vi postgresql.conf[postgreso19c data]$ vi pg_hba.conf[postgreso19c data]$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile stopwaiting for server to shut down.... doneserver stopped[postgreso19c data]$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile startwaiting for server to start.... doneserver started2.3.9 一键安装PostgreSQL源码编译的脚本Shell脚本源码地址Giteehttps://gitee.com/raymond9/shellGithubhttps://github.com/raymond999999/shell可以去上面的Gitee或Github代码仓库拉取脚本。[rootrocky10 ~]# 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 #********************************************************************************** COLORecho -e \\033[01;31m END\033[0m os(){ . /etc/os-release MAIN_NAMEsed -rn /^NAME/s.*([[:alpha:]]).*$\1p /etc/os-release if [ ${MAIN_NAME} Kylin ];then MAIN_VERSION_IDsed -rn /^VERSION_ID/s.*([[:alpha:]])(.*)$\2p /etc/os-release else MAIN_VERSION_IDsed -rn /^VERSION_ID/s.*?([0-9])\.?.*?\1p /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_USERpostgres POSTGRESQL_VERSION17.6 POSTGRESQL_URLhttps://ftp.postgresql.org/pub/source/v${POSTGRESQL_VERSION}/ POSTGRESQL_FILEpostgresql-${POSTGRESQL_VERSION}.tar.gz DB_USER_PASSWORD123456 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_DIRecho ${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 pgstartpg_ctl -D ${DATA_DIR} start alias pgstoppg_ctl -D ${DATA_DIR} stop alias pgrestartpg_ctl -D ${DATA_DIR} restart alias pgstatuspg_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] DescriptionPostgreSQL database server Afternetwork.target [Service] User${DB_USER} Group${DB_USER} ExecStart${INSTALL_DIR}/bin/postgres -D ${DATA_DIR} ExecReload/bin/kill -HUP \$MAINPID [Install] WantedBymulti-user.target EOF else cat /usr/lib/systemd/system/postgresql.service EOF [Unit] DescriptionPostgreSQL database server Afternetwork.target [Service] User${DB_USER} Group${DB_USER} ExecStart${INSTALL_DIR}/bin/postgres -D ${DATA_DIR} ExecReload/bin/kill -HUP \$MAINPID [Install] WantedBymulti-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