postgresql installation and deployment
Steps to build the environment
The complete set of environment is as follows:
PostgreSQL 9.6 SUSE environment construction (1)
PostgreSQL master-slave asynchronous streaming replication configuration (2)
PostgreSQL master-slave asynchronous and synchronous streaming replication configuration (3)
Check and configure the postgres user (if the environment has a current user)
clw-db1:~ # cat /etc/passwd | grep postgres
or
clw-db1:~ # id postgres
id: postgres: No such user
Create an OS group and account for postgres
clw-db1:~ # groupadd -g 10000 postgres
clw-db1:~ # useradd -g 10000 -u 10000 postgres
verify user
clw-db1:~ # id postgres
uid=10000(postgres) gid=10000(postgres) groups=16(dialout),33(video),10000(postgres)
Create the postgres user home directory to store the postgres user records (bash, profile, shell, etc.)
clw-db1:~ # mkdir /home/postgres
clw-db1:~ # chown postgres.postgres /home/postgres/
clw-db1:~ # usermod -d /home/postgres postgres
Install PostgreSQL 9.6
Upload source code and dependencies
Unpack PostgreSQL 9.6.8
clw-db1:~/pgsoft # tar -xf readline-7.0.tar.gz
clw-db1:~/pgsoft # tar -xf zlib-1.2.11.tar.gz
clw-db1:~/pgsoft # tar -xf postgresql-9.6.8.tar.gz
Install readline dependencies
clw-db1:~/pgsoft/readline-7.0 # ./configure
clw-db1:~/pgsoft/readline-7.0 # make && make install
Install zlib dependencies
clw-db1:~/pgsoft/zlib-1.2.11 # make
clw-db1:~/pgsoft/zlib-1.2.11 # make install
Install PostgreSQL 9.6.8 , this installation ignores the readline (record historical sql command query) dependency
clw-db1:~/pgsoft/postgresql-9.6.8 # ./configure --prefix=/opt/pgsql-9.6 --without-readline
View the number of CPU cores of the machine, process indicates that the number of CPU cores starts from 0
cat /proc/cpuinfo |grep less
The readline-devel package is not found , skip it temporarily; compile with the -j 8
specified 8 cores ;
clw-db1:~/pgsoft/postgresql-9.6.8 # make world -j 8
Seeing "PostgreSQL, contrib, and documentation successfully made. Ready to install." indicates that the compilation was successful.
clw-db1:~/pgsoft/postgresql-9.6.8 # make install-world
-world means to install all files in this directory
See "PostgreSQL, contrib, and documentation installation complete." The installation is successful.
Check the installation version and path
clw-db1:~/pgsoft/postgresql-9.6.8 # /opt/pgsql-9.6/bin/postgres --version
postgres (PostgreSQL) 9.6.8
Initialize the data directory
Create data and related directories
clw-db1:~ # mkdir -p /pgdata/9.6/poc/{data,archive,scripts,backup}
Directory name can be customized
data : 数据库存放目录 必须
archieve : 日志归档存放目录,非必须
scripts : 脚本存放目录(故障转移、日志清理等)非必需
backup : 备份存放目录 非必需
The owner of the configuration directory ("." is equivalent to ":")
clw-db1:~ # chown -R postgres.postgres /pgdata/9.6
Indicates that all files in the /pgdata/9.6 directory belong to the postgres user and group , -R means recursion
Check the data directory, "-" means it will automatically switch to the home directory of the current user
clw-db1:~ # su - postgres
postgres@clw-db1:~>
postgres@clw-db1:~> pwd
/home/postgres
postgres@clw-db1:/pgdata/9.6/poc> cd /pgdata/9.6/poc/
postgres@clw-db1:/pgdata/9.6/poc>
postgres@clw-db1:/pgdata/9.6/poc> ll
total 16
drwxr-xr-x 2 postgres postgres 4096 May 3 15:44 archive
drwxr-xr-x 2 postgres postgres 4096 May 3 15:44 backup
drwxr-xr-x 2 postgres postgres 4096 May 3 15:44 data
drwxr-xr-x 2 postgres postgres 4096 May 3 15:44 scripts
Initialize the data directory
postgres@clw-db1:/pgdata/9.6/poc> /opt/pgsql-9.6/bin/initdb -D /pgdata/9.6/poc/data/ -E UTF-8 --locale=zh_CN.UTF-8
-D 表示指定数据库存放目录
-E 表示 指定字符集编码
The permissions of the data directory will be automatically modified to 0700, and you can also configure it manually before this step.
postgres@clw-db1:/pgdata/9.6/poc> ll
total 16
drwxr-xr-x 2 postgres postgres 4096 May 3 15:44 archive
drwxr-xr-x 2 postgres postgres 4096 May 3 15:44 backup
drwx------ 19 postgres postgres 4096 May 3 15:54 data
drwxr-xr-x 2 postgres postgres 4096 May 3 15:44 scripts
Manual configuration
chmod 0700 /data
start and stop
Modify the parameters that must be restarted to take effect and start
postgres@clw-db1:~> vim /pgdata/9.6/poc/data/postgresql.conf
Edit the configuration file and modify the following parameters as follows:
listen_addresses = '*'
port = 5432
max_connections = 100
max_connections = 200
superuser_reserved_connections = 10
shared_buffers = 32GB
maintenance_work_mem = 2GB
#replacement_sort_tuples = 150000
shared_preload_libraries = 'pg_stat_statements'
wal_level = logical
archive_mode = on
archive_command = '/bin/true'
max_wal_senders = 10
max_replication_slots = 10
hot_standby = on
random_page_cost = 1.1
#cpu_tuple_cost = 0.01
effective_cache_size = 64GB
log_destination = 'csvlog'
logging_collector = on
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
Start PG Server
postgres@clw-db1:~> /opt/pgsql-9.6/bin/pg_ctl -D /pgdata/9.6/poc/data/ start
server starting
postgres@clw-db1:~> LOG: redirecting log output to logging collector process
Seeing the following line indicates that the startup was successful:
2018-05-03 16:19:00.067 CST,,,20626,,5aeac5f2.5092,2,,2018-05-03 16:18:58 CST,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""
Create the necessary databases
postgres@clw-db1:~> /opt/pgsql-9.6/bin/psql -p 5432 -U postgres postgres
psql (9.6.8)
Type "help" for help.
postgres=#
postgres=# CREATE DATABASE pocdb;
CREATE DATABASE
Create user:
postgres=# \c pocdb
You are now connected to database "pocdb" as user "postgres".
pocdb=#
pocdb=#
pocdb=# CREATE USER repl ENCRYPTED PASSWORD '123456' REPLICATION;
CREATE ROLE
Authenticate user:
pocdb=# \du+
List of roles
Role name | Attributes | Member of | Description
-----------+------------------------------------------------------------+-----------+-------------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} |
repl | Replication | {} |
exit the connection
pocdb=# \q
postgres@clw-db1:~>
finish installation
本教程主要便于后期多环境部署,没有配置环境变量,故每次启动、停止等命令需要指定具体目录,详细配置环境
变量可参考安装PostgreSQL 10.x的安装教程。