PostgreSQL installation manual under Linux (CentOS7)
Article directory
- 1. Prepare PostgreSQL
- 2. Install PostgreSQL
-
- 2.1 Unzip the installation package
- 2.2 Compile PG
- 2.3 View the PG installation directory
- 2.4 Configure PG environment variables
- 2.5 View PG version
- 2.6 Create postgres user
- 2.7 Create the PG database data storage directory
- 2.8 Authorize PG database data storage directory
- 2.9 Switch postgres user
- 2.10 Initialize PG database
- 2.11 Start PG database service
- 2.12 Check PG database service status
- 2.13 Log in to PG database
- 2.14 View existing database
- 2.15 Check PG database version
- 2.16 Configure the PG database postgres user password
- 2.17 Exit PG database
- 2.18 Stop PG database service
- 2.19 Check PG database service status
- 3. Configure PostgreSQL
-
- 3.1 Modify key configuration files:
- 3.2 Restart the PG database
- 3.3 Log in to the PG database locally
- 3.4 Remote connection to PG database
- 3.5 Stop PG database
- 3.6 Write PG database system service configuration file
- 3.7 Load system configuration file
- 3.8 View PG database system services
- 3.9 Start the PG database system service
- 3.10 Set the PG database system service to start automatically at boot
- 3.11 Stop PG database system service
1. Prepare PostgreSQL
Download address:
https://www.postgresql.org/ftp/source/v13.10/Source
code package:
https://ftp.postgresql.org/pub/source/v13.10/postgresql-13.10.tar.gz
Since the download method provided by the official website is overseas and the domestic download speed is slow,
we use the domestic Alibaba Cloud mirror station to download.
Download address:
https://mirrors.aliyun.com/postgresql/source/v13.10/Source
code package:
https://mirrors. aliyun.com/postgresql/source/v13.10/postgresql-13.10.tar.gz
Prerequisite: There is /opt/software directory.
If not, you need to create it.
mkdir /opt/software
Download on the server: (We use Alibaba Cloud to download)
wget https://mirrors.aliyun.com/postgresql/source/v13.10/postgresql-13.10.tar.gz -P /opt/software
[root@dw3 ~]# wget https://mirrors.aliyun.com/postgresql/source/v13.10/postgresql-13.10.tar.gz -P /opt/software
--2023-04-09 22:06:18-- https://mirrors.aliyun.com/postgresql/source/v13.10/postgresql-13.10.tar.gz
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 8.132.50.95, 8.132.50.94, 8.132.50.96, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|8.132.50.95|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:28117237 (27M) [application/octet-stream]
正在保存至: “/opt/software/postgresql-13.10.tar.gz”
100%[======================================>] 28,117,237 5.44MB/s 用时 5.0s
2023-04-09 22:06:23 (5.41 MB/s) - 已保存 “/opt/software/postgresql-13.10.tar.gz” [28117237/28117237])
[root@dw3 ~]#
2. Install PostgreSQL
2.1 Unzip the installation package
(1) First create the directory where you will store it after decompression
[root@dw3 ~]# mkdir -p /opt/module/
(2) Decompress again
[root@dw3 ~]# tar -zxf /opt/software/postgresql-13.10.tar.gz -C /opt/module/
[root@dw3 ~]#
(3) Enter the decompressed directory
[root@dw3 ~]# cd /opt/module/postgresql-13.10/
[root@dw3 postgresql-13.10]# ls
aclocal.m4 configure contrib doc HISTORY Makefile src
config configure.in COPYRIGHT GNUmakefile.in INSTALL README
[root@dw3 postgresql-13.10]#
2.2 Compile PG
Note:
Before compiling PG, install two dependency packages first, otherwise you will encounter errors later.
yum install readline-devel zlib-devel -y
The following is the detailed compilation process (if any errors are encountered)
[root@dw3 postgresql-13.10]# ./configure --prefix=/usr/local/pg13
Encountered an error:
If you have readline already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
[root@dw3 postgresql-13.10]#
How to solve?
The above error message explains: The operating system lacks the readline-devel package, so let’s install it
yum install readline-devel -y
[root@dw3 postgresql-13.10]# yum install readline-devel -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 readline-devel.x86_64.0.6.2-11.el7 将被 安装
--> 正在处理依赖关系 ncurses-devel,它被软件包 readline-devel-6.2-11.el7.x86_64 需要
--> 正在检查事务
---> 软件包 ncurses-devel.x86_64.0.5.9-14.20130511.el7_4 将被 安装
--> 解决依赖关系完成
依赖关系解决
===============================================================================
Package 架构 版本 源 大小
===============================================================================
正在安装:
readline-devel x86_64 6.2-11.el7 base 139 k
为依赖而安装:
ncurses-devel x86_64 5.9-14.20130511.el7_4 base 712 k
事务概要
===============================================================================
安装 1 软件包 (+1 依赖软件包)
总下载量:851 k
安装大小:2.4 M
Downloading packages:
(1/2): readline-devel-6.2-11.el7.x86_64.rpm | 139 kB 00:00
(2/2): ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm | 712 kB 00:00
-------------------------------------------------------------------------------
总计 4.2 MB/s | 851 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
正在安装 : ncurses-devel-5.9-14.20130511.el7_4.x86_64 1/2
正在安装 : readline-devel-6.2-11.el7.x86_64 2/2
验证中 : readline-devel-6.2-11.el7.x86_64 1/2
验证中 : ncurses-devel-5.9-14.20130511.el7_4.x86_64 2/2
已安装:
readline-devel.x86_64 0:6.2-11.el7
作为依赖被安装:
ncurses-devel.x86_64 0:5.9-14.20130511.el7_4
完毕!
[root@dw3 postgresql-13.10]#
Next we recompile PG
[root@dw3 postgresql-13.10]# ./configure --prefix=/usr/local/pg13
I encountered another error:
configure: error: zlib library not found
If you have zlib already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-zlib to disable zlib support.
[root@dw3 postgresql-13.10]#
How to solve?
The above error message explains: The operating system lacks the zlib-devel package, so let’s install it
yum install zlib-devel -y
[root@dw3 postgresql-13.10]# yum install zlib-devel -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 zlib-devel.x86_64.0.1.2.7-21.el7_9 将被 安装
--> 解决依赖关系完成
依赖关系解决
===============================================================================
Package 架构 版本 源 大小
===============================================================================
正在安装:
zlib-devel x86_64 1.2.7-21.el7_9 updates 50 k
事务概要
===============================================================================
安装 1 软件包
总下载量:50 k
安装大小:132 k
Downloading packages:
zlib-devel-1.2.7-21.el7_9.x86_64.rpm | 50 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : zlib-devel-1.2.7-21.el7_9.x86_64 1/1
验证中 : zlib-devel-1.2.7-21.el7_9.x86_64 1/1
已安装:
zlib-devel.x86_64 0:1.2.7-21.el7_9
完毕!
[root@dw3 postgresql-13.10]#
下面我们重新编译PG
[root@dw3 postgresql-13.10]# ./configure --prefix=/usr/local/pg13
The successful compilation is as follows:
Then
make compile
[root@dw3 postgresql-13.10]# make
Since the CPU is a single core, the -j parameter does not need to be used
make install installation
[root@dw3 postgresql-13.10]# make install
Compilation and installation completed
2.3 View the PG installation directory
First enter the installation directory
cd /usr/local/pg13/
[root@dw3 postgresql-13.10]# cd /usr/local/pg13/
then view
[root@dw3 pg13]# ls
bin include lib share
[root@dw3 pg13]#
2.4 Configure PG environment variables
vim /etc/profile.d/pg.sh
Copy the following content to the file
export PG_HOME=/usr/local/pg13
export PATH=$PG_HOME/bin:$PATH
export LD_LIBRARY_PATH=$PG_HOME/lib
Notice:
LD_LIBRARY_PATH is the location where the dynamic library is stored after the PG database source code is compiled.
[root@dw3 pg13]# vim /etc/profile.d/pg.sh
[root@dw3 pg13]#
[root@dw3 pg13]# cat /etc/profile.d/pg.sh
export PG_HOME=/usr/local/pg13
export PATH=$PG_HOME/bin:$PATH
export LD_LIBRARY_PATH=$PG_HOME/lib
[root@dw3 pg13]#
[root@dw3 pg13]# source /etc/profile.d/pg.sh
[root@dw3 pg13]#
2.5 View PG version
psql -V
[root@dw3 pg13]# psql -V
psql (PostgreSQL) 13.10
[root@dw3 pg13]#
2.6 Create postgres user
useradd postgres
[root@dw3 pg13]# useradd postgres
[root@dw3 pg13]#
2.7 Create the PG database data storage directory
mkdir -p /usr/local/pg13/data
[root@dw3 pg13]# mkdir -p /usr/local/pg13/data
[root@dw3 pg13]# ls
bin data include lib share
[root@dw3 pg13]#
2.8 Authorize PG database data storage directory
chown -R postgres. /usr/local/pg13
[root@dw3 pg13]# chown -R postgres. /usr/local/pg13
[root@dw3 pg13]#
2.9 Switch postgres user
su postgres
[root@dw3 pg13]# su postgres
[postgres@dw3 pg13]$
[postgres@dw3 pg13]$ ls -l
总用量 20
drwxr-xr-x 2 postgres postgres 4096 4月 10 00:03 bin
drwxr-xr-x 2 postgres postgres 4096 4月 10 00:16 data
drwxr-xr-x 4 postgres postgres 4096 4月 10 00:03 include
drwxr-xr-x 4 postgres postgres 4096 4月 10 00:03 lib
drwxr-xr-x 3 postgres postgres 4096 4月 10 00:03 share
[postgres@dw3 pg13]$
2.10 Initialize PG database
./bin/initdb -D /usr/local/pg13/data
[postgres@dw3 pg13]$ ./bin/initdb -D /usr/local/pg13/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 "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".
Data page checksums are disabled.
fixing permissions on existing directory /usr/local/pg13/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 ... 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
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:
./bin/pg_ctl -D /usr/local/pg13/data -l logfile start
[postgres@dw3 pg13]$
2.11 Start PG database service
[postgres@dw3 pg13]$ ./bin/pg_ctl -D /usr/local/pg13/data -l logfile start
waiting for server to start.... done
server started
[postgres@dw3 pg13]$
2.12 Check PG database service status
[postgres@dw3 pg13]$ ./bin/pg_ctl -D /usr/local/pg13/data -l logfile status
pg_ctl: server is running (PID: 15401)
/usr/local/pg13/bin/postgres "-D" "/usr/local/pg13/data"
[postgres@dw3 pg13]$
2.13 Log in to PG database
psql
[postgres@dw3 pg13]$ psql
psql (13.10)
Type "help" for help.
postgres=#
2.14 View existing database
\l
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=#
2.15 Check PG database version
postgres=# select version();
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 13.10 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
(1 row)
postgres=#
2.16 Configure the PG database postgres user password
There is no password by default, set the password to: Pg_10.24!
\password
postgres=# \password
Enter new password for user "postgres":
Enter it again:
postgres=#
2.17 Exit PG database
\q
postgres=# \q
[postgres@dw3 pg13]$
2.18 Stop PG database service
[postgres@dw3 pg13]$ ./bin/pg_ctl -D /usr/local/pg13/data -l logfile stop
waiting for server to shut down.... done
server stopped
[postgres@dw3 pg13]$
2.19 Check PG database service status
[postgres@dw3 pg13]$ ./bin/pg_ctl -D /usr/local/pg13/data -l logfile status
pg_ctl: no server running
[postgres@dw3 pg13]$
3. Configure PostgreSQL
3.1 Modify key configuration files:
(1) Configure user permissions and remote permissions
vim /usr/local/pg13/data/pg_hba.conf
Configure the following
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
(2) Modify the access address and maximum number of connections
vim /usr/local/pg13/data/postgresql.conf
Configure the following
listen_addresses = '*' # what IP address(es) to listen on;
port = 5432 # (change requires restart)
max_connections = 1000 # (change requires restart)
logging_collector = on # Enable capturing of stderr and csvlog
log_directory = '/usr/local/pg13/log' # directory where log files are written,
[postgres@dw3 pg13]$ ll /usr/local/pg13/data/pg_hba.conf
-rw------- 1 postgres postgres 4760 4月 10 00:25 /usr/local/pg13/data/pg_hba.conf
[postgres@dw3 pg13]$
[postgres@dw3 pg13]$ ll /usr/local/pg13/data/postgresql.conf
-rw------- 1 postgres postgres 28093 4月 10 00:25 /usr/local/pg13/data/postgresql.conf
[postgres@dw3 pg13]$
3.2 Restart the PG database
[postgres@dw3 pg13]$ pg_ctl -D /usr/local/pg13/data restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2023-04-10 01:08:22.214 CST [15646] LOG: redirecting log output to logging collector process
2023-04-10 01:08:22.214 CST [15646] HINT: Future log output will appear in directory "/usr/local/pg13/log".
done
server started
[postgres@dw3 pg13]$
[postgres@dw3 pg13]$ ll
总用量 28
drwxr-xr-x 2 postgres postgres 4096 4月 10 00:03 bin
drwx------ 19 postgres postgres 4096 4月 10 01:08 data
drwxr-xr-x 4 postgres postgres 4096 4月 10 00:03 include
drwxr-xr-x 4 postgres postgres 4096 4月 10 00:03 lib
drwx------ 2 postgres postgres 4096 4月 10 01:08 log
-rw------- 1 postgres postgres 3911 4月 10 01:08 logfile
drwxr-xr-x 3 postgres postgres 4096 4月 10 00:03 share
[postgres@dw3 pg13]$ ll log
总用量 4
-rw------- 1 postgres postgres 605 4月 10 01:08 postgresql-2023-04-10_010822.log
[postgres@dw3 pg13]$
3.3 Log in to the PG database locally
Note: You need to enter a password to log in this time
[postgres@dw3 pg13]$ psql -U postgres -d postgres -h 127.0.0.1 -p 5432
Password for user postgres:
psql (13.10)
Type "help" for help.
postgres=#
3.4 Remote connection to PG database
Server public IP: 47.92.93.96
PG default port number: 5432
PG remote user: postgres
PG remote user password: Pg_10.24!
Use remote tools to connect to the test, taking the DataGrip tool as an example:
3.5 Stop PG database
[postgres@dw3 pg13]$ ./bin/pg_ctl -D /usr/local/pg13/data -l logfile stop
waiting for server to shut down.... done
server stopped
[postgres@dw3 pg13]$
3.6 Write PG database system service configuration file
Note: root
Written under user
vim /lib/systemd/system/postgres.service
[root@dw3 pg13]# cat /lib/systemd/system/postgres.service
[Unit]
Description=postgresql-13.10
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
Type=forking
User=postgres
Group=postgres
ExecStart=/usr/local/pg13/bin/pg_ctl -D /usr/local/pg13/data start
ExecStop=/usr/local/pg13/bin/pg_ctl -D /usr/local/pg13/data stop
PrivateTmp=true
[root@dw3 pg13]#
3.7 Load system configuration file
[root@dw3 pg13]# systemctl daemon-reload
3.8 View PG database system services
[root@dw3 pg13]# systemctl status postgres.service
● postgres.service - postgresql-13.10
Loaded: loaded (/usr/lib/systemd/system/postgres.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@dw3 pg13]#
3.9 Start the PG database system service
[root@dw3 ~]# systemctl start postgres.service
[root@dw3 ~]# systemctl status postgres.service
● postgres.service - postgresql-13.10
Loaded: loaded (/usr/lib/systemd/system/postgres.service; disabled; vendor preset: disabled)
Active: active (running) since 一 2023-04-10 01:38:05 CST; 2s ago
Process: 16123 ExecStart=/usr/local/pg13/bin/pg_ctl -D /usr/local/pg13/data start (code=exited, status=0/SUCCESS)
Main PID: 16125 (postgres)
Tasks: 8
Memory: 41.0M
CGroup: /system.slice/postgres.service
├─16125 /usr/local/pg13/bin/postgres -D /usr/local/pg13/data
├─16126 postgres: logger
├─16128 postgres: checkpointer
├─16129 postgres: background writer
├─16130 postgres: walwriter
├─16131 postgres: autovacuum launcher
├─16132 postgres: stats collector
└─16133 postgres: logical replication launcher
4月 10 01:38:05 dw3 systemd[1]: Starting postgresql-13.10...
4月 10 01:38:05 dw3 pg_ctl[16123]: waiting for server to start....2023-04-10 01:38:05.439 CST [1612...ocess
4月 10 01:38:05 dw3 pg_ctl[16123]: 2023-04-10 01:38:05.439 CST [16125] HINT: Future log output wil...log".
4月 10 01:38:05 dw3 systemd[1]: Started postgresql-13.10.
Hint: Some lines were ellipsized, use -l to show in full.
[root@dw3 ~]#
3.10 Set the PG database system service to start automatically at boot
[root@dw3 ~]# systemctl enable postgres.service
Created symlink from /etc/systemd/system/multi-user.target.wants/postgres.service to /usr/lib/systemd/system/postgres.service.
[root@dw3 ~]#
[root@dw3 ~]# systemctl status postgres.service
● postgres.service - postgresql-13.10
Loaded: loaded (/usr/lib/systemd/system/postgres.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2023-04-10 01:38:05 CST; 2min 56s ago
Main PID: 16125 (postgres)
CGroup: /system.slice/postgres.service
├─16125 /usr/local/pg13/bin/postgres -D /usr/local/pg13/data
├─16126 postgres: logger
├─16128 postgres: checkpointer
├─16129 postgres: background writer
├─16130 postgres: walwriter
├─16131 postgres: autovacuum launcher
├─16132 postgres: stats collector
└─16133 postgres: logical replication launcher
4月 10 01:38:05 dw3 systemd[1]: Starting postgresql-13.10...
4月 10 01:38:05 dw3 pg_ctl[16123]: waiting for server to start....2023-04-10 01:38:05.439 CST [16125] LOG: redirecting log o... process
4月 10 01:38:05 dw3 pg_ctl[16123]: 2023-04-10 01:38:05.439 CST [16125] HINT: Future log output will appear in directory "/us...13/log".
4月 10 01:38:05 dw3 systemd[1]: Started postgresql-13.10.
Hint: Some lines were ellipsized, use -l to show in full.
[root@dw3 ~]#
3.11 Stop PG database system service
[root@dw3 ~]# systemctl stop postgres.service
[root@dw3 ~]# systemctl status postgres.service
● postgres.service - postgresql-13.10
Loaded: loaded (/usr/lib/systemd/system/postgres.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 一 2023-04-10 01:48:55 CST; 1s ago
Process: 16303 ExecStop=/usr/local/pg13/bin/pg_ctl -D /usr/local/pg13/data stop (code=exited, status=0/SUCCESS)
Process: 16285 ExecStart=/usr/local/pg13/bin/pg_ctl -D /usr/local/pg13/data start (code=exited, status=0/SUCCESS)
Main PID: 16287 (code=exited, status=0/SUCCESS)
4月 10 01:48:44 dw3 systemd[1]: Starting postgresql-13.10...
4月 10 01:48:44 dw3 pg_ctl[16285]: waiting for server to start....2023-04-10 01:48:44.728 CST [16287] LOG: redirecting log o... process
4月 10 01:48:44 dw3 pg_ctl[16285]: 2023-04-10 01:48:44.728 CST [16287] HINT: Future log output will appear in directory "/us...13/log".
4月 10 01:48:44 dw3 systemd[1]: Started postgresql-13.10.
4月 10 01:48:55 dw3 systemd[1]: Stopping postgresql-13.10...
4月 10 01:48:55 dw3 systemd[1]: Stopped postgresql-13.10.
Hint: Some lines were ellipsized, use -l to show in full.
[root@dw3 ~]#
This ends the PG database installation!
See you next time, bye!