从源代码中安装最新版本的 PostgreSQL 12.x 到 Cent OS 8
首先
在通过”最低限的安装”方式安装的CentOS-8.1.1911-x86_64环境中,我们将从源代码安装最新版本的PostgreSQL12.x。
整个操作系统的更新
dnf -y update
安装必要的软件
dnf -y install gcc gcc-c++ make wget tar zlib-devel readline-devel
安装PostgreSQL
请以root权限进行操作。
创建用户
创建一个名为 “postgres” 的用户,用于编译和执行 PostgreSQL。
useradd postgres
passwd postgres
下载源代码。
cd /usr/local/src/
wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.gz
进行
tar xzf postgresql-12.2.tar.gz
更换所有者
chown -R postgres /usr/local/src/postgresql-12.2
创建安装目录(同时设定所有者)
mkdir /usr/local/pgsql12.2
chown postgres /usr/local/pgsql12.2
创建符号链接
ln -s /usr/local/pgsql12.2 /usr/local/pgsql
解除方法在这里。
解除方法请参考这里。
请点击这里查看解除方法。
unlink /usr/local/pgsql
切换用户并移动目录。
su - postgres
cd /usr/local/src/postgresql-12.2
创建Makefile
我正在指定安装路径。
./configure --prefix=/usr/local/pgsql
编译和安装
make
make install
设置环境变量
vi ~/.bash_profile
将以下内容写在底部并保存。
export PATH="$PATH":/usr/local/pgsql/bin
export POSTGRES_HOME=/usr/local/pgsql
export PGLIB=$POSTGRES_HOME/lib
export PGDATA=$POSTGRES_HOME/data
export MANPATH="$MANPATH":$POSTGRES_HOME/man
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"
环境变量的应用
. ~/.bash_profile
将数据库进行初始化
initdb --no-locale
编辑设置文件
postgresql.conf 是 PostgreSQL 数据库的配置文件。
vi /usr/local/pgsql/data/postgresql.conf
リモートアクセス可能にする。
#listen_addresses = 'localhost'
↓
listen_addresses = '*'
明示的にポートを指定する。
#port = 5432
↓
port = 5432
ログを有効にする。
#logging_collector = off
↓
logging_collector = on
ログのファイル名を変更する。
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
↓
log_filename = 'postgresql-%d.log'
#log_line_prefix = ''
↓
log_line_prefix = '%t [%p] '
pg_hba.conf可以进行重写为:汇编.hba配置文件。
vi /usr/local/pgsql/data/pg_hba.conf
请指定listen_addresses的范围。
以下是一个示例,请根据您的环境进行相应调整。
host all all 127.0.0.1/32 trust
↓
host all all 192.168.1.0/24 trust
启动
pg_ctl start -w
创建DB管理用户
createuser -s -d -r -P pgsa
确认创建的用户
psql -U postgres
\du
[postgres@localhost postgresql-12.2]$ psql -U postgres
psql (12.2)
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
pgsa | Superuser, Create role, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
用”结束”来表示结束
防火墙的设置
请以 root 权限进行设置。
su -
firewall-cmd --add-service=postgresql --zone=public --permanent
systemctl restart firewalld
SELinux的配置
setsebool -P postgresql_can_rsync on
setsebool -P postgresql_selinux_transmit_client_label on
setsebool -P selinuxuser_postgresql_connect_enabled on
确认
getsebool -a | grep postgresql
postgresql_can_rsync --> on
postgresql_selinux_transmit_client_label --> on
postgresql_selinux_unconfined_dbadm --> on
postgresql_selinux_users_ddl --> on
selinuxuser_postgresql_connect_enabled --> on
自动启动设置
创建服务定义文件。
vi /usr/lib/systemd/system/postgresql.service
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades. If you want to customize, the
# best way is to create a file "/etc/systemd/system/postgresql.service",
# containing
# .include /lib/systemd/system/postgresql.service
# ...make your changes here...
# For more info about custom unit files, see
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F
# For example, if you want to change the server's port number to 5433,
# create a file named "/etc/systemd/system/postgresql.service" containing:
# .include /lib/systemd/system/postgresql.service
# [Service]
# Environment=PGPORT=5433
# This will override the setting appearing below.
# Note: changing PGPORT or PGDATA will typically require adjusting SELinux
# configuration as well; see /usr/share/doc/postgresql-*/README.rpm-dist.
# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-setup.
# Note: in F-17 and beyond, /usr/lib/... is recommended in the .include line
# though /lib/... will still work.
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
# Port number for server to listen on
Environment=PGPORT=5432
# Location of database directory
Environment=PGDATA=/usr/local/pgsql/data
# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
#ExecStartPre=/usr/local/pgsql/bin/postgresql-check-db-dir ${PGDATA} <=このモジュールは存在しないのでコメント
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300
[Install]
WantedBy=multi-user.target
自动启动执行
systemctl daemon-reload
systemctl enable postgresql
确认行动
systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2020-05-09 07:45:17 EDT; 28s ago
Process: 1103 ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, sta>
Main PID: 1144 (postgres)
Tasks: 8 (limit: 23922)
Memory: 19.9M
CGroup: /system.slice/postgresql.service
├─1144 /usr/local/pgsql12.2/bin/postgres -D /usr/local/pgsql/data -p 5432
├─1422 postgres: logger
├─1483 postgres: checkpointer
├─1484 postgres: background writer
├─1485 postgres: walwriter
├─1486 postgres: autovacuum launcher
├─1487 postgres: stats collector
└─1488 postgres: logical replication launcher