Kylin v10 源码安装Postgres-13
背景
...
环境
- OS: Kylin v10 SP2/SP3
- PostgreSQL: 13.12
- Platform: Amd64/Arm64
安装步骤
1、下载二进制包
# export version=13.12
# cd /usr/local/src
# wget https://ftp.postgresql.org/pub/source/v${version}/postgresql-${version}.tar.bz22、解压
# tar axvf postgresql-${version}.tar.bz23、安装依赖组件
# yum install readline-devel zlib-devel如果是Debian 分支:apt install build-essential pkg-config libicu-dev bison flex zlib1g-dev libreadline-dev libssl-dev liblz4-dev libzstd-dev uuid-dev
4、创建用户
# groupadd -g 5432 postgres && useradd -u 5432 -g postgres postgres
# passwd postgres
5、软件编译安装
cd postgresql-${version}
./configure --prefix=/usr/local/postgresql-${version} \
--with-pgport=5432 \
-with-openssl \
--with-lz4 \
--with-zstd
make -j $(nproc --all)
make install
//安装contrib 模块(可选)
cd contrib && make && make install
cd /usr/local/ && ln -svf postgresql-${version} postgresql
6、创建数据目录并分配权限
install -d /data/pgsql/LOG \
/data/pgsql/DATA \
-o postgres \
-g postgres
chown -R postgres:postgres /data/pgsql \
/usr/local/postgresql-${version}
chmod -R 0700 /data/pgsql/7、初始化数据
# su - postgres
$ /usr/local/postgresql/bin/initdb -D /data/pgsql/DATA/8、添加环境变量
- 单用户
$ vim .bashrc
...
# Postgresql
export LD_LIBRARY_PATH=/usr/local/postgresql/lib:$LD_LIBRARY_PATH
export PATH=/usr/local/postgresql/bin/:$PATH- 全局
# cat > /etc/profile.d/postgresql.sh<<EOF
export LD_LIBRARY_PATH=/usr/local/postgresql/lib:\$LD_LIBRARY_PATH
export PATH=/usr/local/postgresql/bin/:\$PATH
EOF
9、创建systemd 服务文件
cat >/etc/systemd/system/postgresql.service<<EOF
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
LimitNOFILE=1024000
Environment=PGSTARTTIMEOUT=270
Environment=PGLOG=/data/pgsql/LOG/startup.log
Environment=PGDATA=/data/pgsql/DATA
ExecStart=/usr/local/postgresql/bin/pg_ctl start -w -D \$PGDATA -l \$PGLOG
ExecStop=/usr/local/postgresql/bin/pg_ctl stop -m fast -w -D \$PGDATA
ExecReload=/usr/local/postgresql/bin/pg_ctl reload -D \$PGDATA
TimeoutSec=300
[Install]
WantedBy=multi-user.target
EOF10、基本配置
# vim /data/pgsql/DATA/postgresql.conf
listen_addresses = '*'
port = 5432
max_connections = 8848
shared_buffers = 4096MB
effective_cache_size = 8GB
wal_keep_size= 2048MB
# vim pg_hba.conf
host all all 0.0.0.0/0 scram-sha-256
host replication all ... trust11、服务启动
# systemctl enable postgresql.service --now12、更新超管密码
# sudo su - postgres
$ psql -c "alter user postgres with password 'VQIU@8848'"
// 或者创建一个超管用户
CREATE ROLE vqiu_conn WITH LOGIN SUPERUSER PASSWORD 'VQIU@8848';13、使用psql 命令测试
$ psql
psql (13.12)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)流复制配置
1、创建复制专属账号 repl_user
# su - postgres
$ createuser --replication -P repl_user2、配置ACL
# vim pg_hba.conf
...
host replication repl_user 从节点IP scram-sha-256
...
// 使用以下方法使其生效
# su - postgres
$ psql
> SELECT pg_reload_conf()3、登陆到从节点,执行以下命令
$ pg_basebackup -R -h 主节点IP -U repl_user -D /data/pgsql/DATA -P提示:如若数据过大,可以加上-c fast参数 。
4、完成后启动服务器即可
# systemctl enable postgresql --now5、使用以下方法来验证主从状态
$ psql -c "select usename, application_name, client_addr, state, sync_priority, sync_state from pg_stat_replication;"提示:上述命令在主节点中运行。
备节点独立
某种原因,需要将数据集群中的备节点独立出来。
1、确认当前节点集群角色
postgres=# SELECT pg_is_in_recovery();
pg_is_in_recovery
------------------
t
(1 row)提示: 返回 t,说明该节点为从节点!
2、 从集群中独立出来
使用pg_ctl promote 工具向 PostgreSQL 的主进程(postmaster)发送 SIGUSR1 信号,触发从节点的提升操作,使其从恢复模式切换到读写模式,成为主节点:
$ pg_ctl promote -D /data/pgsql/DATA
waiting for server to promote.... done
server promoted
3、再次校验角色是否切换成功
再次运行 SELECT pg_is_in_recovery();
postgres=# SELECT pg_is_in_recovery();
pg_is_in_recovery
------------------
f
(1 row)提示: 若返回 f,则表示提升成功,当前节点已为主节点。