一.postgresql数据库安装
1.下载软件包
地址:PostgreSQL: File Browser
2.解压安装
[root@postgresql u01]# tar -zxf postgresql-14.2.tar.gz
安装环境
yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
3.编译
[root@postgresql u01]# cd postgresql-14.2
[root@postgresql postgresql-14.2]# ./configure --prefix=/usr/local/postgresql
4.安装
[root@postgresql postgresql-14.2]# make && make install
5.进入安装后的目录,查看目录结构
[root@VM-8-15-centos ~]# cd /usr/local/postgresql
6、创建目录 data、log
[root@postgresql bin]# mkdir /usr/local/postgresql/data
[root@postgresql bin]# mkdir /usr/local/postgresql/log
7、加入系统环境变量
[root@postgresql bin]#vim /etc/profile
添加如下内容
export PGHOME=/usr/local/postgresql
export PGDATA=/usr/local/postgresql/data
export JAVA_HOME=/u01/jdk1.8.0_201
export CLASSPATH=$:CLASSPATH:$JAVA_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
使配置文件生效
[root@postgresql bin]# source /etc/profile
8、增加用户 postgres 并赋权
[root@postgresql postgresql]# useradd postgres
[root@postgresql postgresql]# chown -R postgres:root /usr/local/postgresql
9、初始化数据库
[root@postgresql ~]# su postgres
[postgres@postgresql root]$ /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/
10.配置文件
[postgres@postgresql root]$ vim /usr/local/postgresql/data/postgresql.conf
[postgres@postgresql root]$ vim /usr/local/postgresql/data/pg_hba.conf
添加
host all all 0.0.0.0/0 trust
11、启动服务
[postgres@postgresql root]$ pg_ctl start -l /usr/local/postgresql/log/pg_server.log
12.查看版本
[postgres@postgresql root]$ psql -V
13.登录数据库
[postgres@postgresql root]$ psql -U postgres -d postgres
13.查看有几个库
postgres=# \l
14.进库
postgres=# \c template1
15.建表
template1=# create table test(name varchar(20),age int);
16 .查看库里面的表
template1=# \d
17.切换库
template1=# \c postgres
18.建表插入数据
postgres=# create table class(id bigint,name varchar(20),age int,sex char(2));
CREATE TABLE
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | class | table | postgres
(1 row)
postgres=# insert into class values(001,'赵华',16,'男');
INSERT 0 1
19.查询数据条数
postgres=# select count(*) from class;
二.pgpool安装
1.下载地址
Downloads - pgpool Wiki
2.解压
[root@sqlserver u01]# tar -zxvf pgpool-II-4.2.9.tar.gz
创建目录
mkdir -p /postgres/pgpool
3.编译
[root@sqlserver pgpool-II-4.2.9]# ./configure --with-pgsql=/postgres/pgpool
若报错configure: error: libpq is not installed or libpq is old
[root@sqlserver ~]# yum install -y postgresql* gcc*
4.安装
进入目录
[root@sqlserver pgpool-recovery]# cd /u01/pgpool-II-4.2.9/src/sql/pgpool-recovery
安装
[root@sqlserver pgpool-recovery]# make && make install
5.更改
[root@sqlserver data]# vim postgresql.conf
更改内容
archive_mode = on
archive_command = 'cp "%p" "/postgres/archivedir" '
max_wal_senders = 10
max_replication_slots = 10
wal_level = replica
创建目录
[root@sqlserver pgpool-recovery]# mkdir -p /postgres/archivedir
6.重启数据库
[postgres@postgresql data]$ pg_ctl restart
7.主库修改postgres的密码、创建流复制用户repl
ALTER USER postgres WITH PASSWORD '123456';
CREATE ROLE pgpool WITH PASSWORD '123456' LOGIN;
CREATE ROLE repl WITH PASSWORD '123456' REPLICATION LOGIN;
postgres=#
postgres=# ALTER USER postgres WITH PASSWORD '123456';
ALTER ROLE
postgres=# CREATE ROLE pgpool WITH PASSWORD '123456' LOGIN;
CREATE ROLE
postgres=# CREATE ROLE repl WITH PASSWORD '123456' REPLICATION LOGIN;
CREATE ROLE
8.创建测试表tb_pgpool
postgres=# CREATE TABLE tb_pgpool ( id serial,age bigint,insertTime timestamp default now());
^
postgres=# insert into tb_pgpool(age) values(1);
查询
postgres=# select * from tb_pgpool;
id | age | inserttime
----+-----+----------------------------
1 | 1 | 2023-01-01 18:52:26.506927
(1 行记录)
9.pgpool配置
查找pgpool.conf.sample-stream
[postgres@postgresql pgpool-II-4.2.9]$ find ./ -name pgpool.conf.sample-stream