一、postgresql 下载
pg官网:postgres.org一般推荐用源码安装,下载 .tar.gz 包
二、安装
本文以12.6版本安装为例:2.1、安装前要求和环境配置
# 1、要求GNU make版本3.80或以上(GNU make有时以名字gmake安装),要测试make版本可以使用以下命令(如果是安装其他版本的pg具体要求make的版本详情见官网对应版本的安装文档):
[root@xl001 ~]# make --version
GNU Make 3.82
# 2、安装依赖包
yum install -y gcc gcc-c++ epel-release llvm5.0 llvm5.0-devel clang libicu-devel perl-ExtUtils-Embed readline readline-devel zlib zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel openldap-devel systemd-devel tcl-devel python-devel
正常情况下,OS已经帮我们把一些包已经装好了,只需要安装以下三个包就好
yum -y install readline-devel zlib-devel gcc
# 3、关闭防火墙
systemctl disable firewalld
systemctl stop firewalld
# 4、创建postgres用户
useradd postgres
passwd postgres # 设置密码
# 5、系统参数调优
# 5.1、修改系统内核参数
vi /etc/sysctl.conf
# 主要是修改三个参数
kernel.shmall=expr `free |grep Mem|awk '{print $2 *1024}'` / `getconf PAGE_SIZE`
kernel.shmmax=free |grep Mem|awk '{print $2 *1024}'
vm.nr_hugepages
参数解释:
kernel.shmall 表示内核可以为共享内存分配的最大页数(pages),这里的“页”是指内核的页面大小,默认通常是 4KB。
kernel.shmmax 设置了单个共享内存段的最大最大字节数
vm.nr_hugepages 参数用于指定系统中可以分配的大页数量
# 其他参数
fs.file-max = 6815744
kernel.sem = 10000 10240000 10000 1024
kernel.shmmni = 819200
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_max = 1048576
fs.aio-max-nr = 40960000
vm.dirty_ratio=20
vm.dirty_background_ratio=3
vm.dirty_writeback_centisecs=3000
vm.dirty_expire_centisecs=500
vm.swappiness=10
vm.min_free_kbytes=524288
sysctl -p #即时生效
# 修改最大进程数和文件句柄数
vim /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
postgres soft nofile 65535
postgres hard nofile 65535
postgres soft nproc 65535
postgres hard nproc 65535
# 6、创建目录
mkdir -p /opt/pgsql-12.6
mkdir -p /opt/pgdata/data
mkdir -p /opt/pgdata/wal
mkdir -p /opt/pgdata/archive
mkdir -p /opt/pgdata/logs
chown -R postgres:postgres /opt/pgsql-12.6/ /opt/pgdata
# 7、配置postgres环境变量
su - postgres
vim .bash_profile
export PGHOME=/opt/pgsql-12.6
export PGDATA=/opt/pgdata/data
export PGHOST=/opt/pgdata/data
export PGUSER=postgres
export PGPORT=5432
export PATH=$HOME/bin:$PGHOME/bin:$PATH
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
source .bash_profile
2.2、安装
# 1、上传安装包并解压
tar -zxvf postgresql-12.6.tar.gz
cd postgresql-12.6
./configure --prefix=/opt/pgsql-12.6 --with-pgport=5432 # 设置安装目录和端口
# 解释
./configure 这一步可以设置一些参数,具体可./configure -h 查看
# 2、安装
gmake world #这个命令是全部功能安装,也可以精细化部署,具体可以参考官网
gmake install-world
chown -R postgres:postgres /opt/pgsql-12.6
# 3、初始化数据库
/opt/pgsql-12.6/bin/initdb -D /opt/pgdata/data
# 4、编辑配置文件pg_hba.conf,此配置控制的是连接数据库相关,文件中配置的ip才可以连接数据库,没配置的都不能连接
vim /opt/pgdata/data/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust # 此行ADDRESS没有值,代表是socket连接
# IPv4 local connections:
host all all 127.0.0.1/32 trust #若这一行没有,则表示在本地不能用tcp/ip方式连接
host all all 0.0.0.0/0 trust
###如果出现两个相同的配置,则下面的不起作用,比如
host all all 192.168.225.0/24 reject
host all all 192.168.225.201/24 MD5 #例如这两个配置,比如有个连接从客户端192.168.225.201过来,读到上面的配置,
#因为192.168.225.201满足192.168.225.0/24,所以连接被拒绝,即使在读到下面一条可以连接,也不会生效了,最终就是连接被拒绝
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
# 5、修改配置文件postgresql.conf
vim /opt/pgdata/dataa/postgresql.con
listen_addresses = 'localhost' #把localhost改为*,默认是只能本地连接
# 6、启动数据库
pg_ctl -D /opt/pgdata start
# pg修改配置文件后可以动态加载,不用重启数据库
pg_ctl -D /opt/pgdata/data reload
# 7、关闭数据库
pg_ctl -D /opt/pgdata stop -m smart|fast|immediate #smart,会等到所有客户端连接都断开始之后才关闭,fast会kill掉所有连接回滚所有还未完成的事务然后关闭(默认方式),immediate相当于直接kill -9进程,不建议
pg_ctl -D /opt/pgdata/data stop -mf
# 8、设置密码
[postgres@xl001 data]$ psql -Upostgres -h127.0.0.1 -dpostgres -p5432 -W
psql (12.6)
Type "help" for help.
postgres=# \password
Enter new password:
Enter it again:
postgres=#
2.3、附一份常用的postgresql.conf配置文件
点击查看代码
listen_addresses = '0.0.0.0'
port = 5432
max_connections = 8000
unix_socket_directories ='/opt/pgdata/data'
unix_socket_group = 'postgres'
unix_socket_permissions = 0700
shared_buffers = 32GB
dynamic_shared_memory_type = posix
wal_level = logical
max_wal_size = 10GB
min_wal_size = 1GB
archive_mode = always
archive_command = 'cp %p /opt/pgdata/mytest/archive/%f'
max_wal_senders = 10
wal_keep_segments = 5120
wal_buffers = -1
wal_log_hints = on
temp_buffers = 8GB
work_mem = 32MB
maintenance_work_mem = 8GB
effective_io_concurrency = 200
max_worker_processes = 16
max_parallel_workers = 16
max_replication_slots = 8
hot_standby = on
synchronous_commit = on
logging_collector = on
checkpoint_completion_target = 0.9
checkpoint_timeout = 15min
log_destination = csvlog
log_directory = '/opt/pgdata/logs'
log_filename = 'postgresql-%Y-%m-%d.log'
log_file_mode = 0600
log_truncate_on_rotation = on
log_rotation_age = 1d
log_min_messages = warning
log_min_duration_statement = 30s
log_checkpoints = on
log_duration = off
log_lock_waits = on
log_statement = 'ddl'
log_timezone = 'PRC'
track_activities = on
track_counts = on
track_io_timing = on
track_functions = all
datestyle = 'iso, mdy'
timezone = 'PRC'
lc_messages = 'en_US.utf8'
lc_monetary = 'en_US.utf8'
lc_numeric = 'en_US.utf8'
lc_time = 'en_US.utf8'
random_page_cost = 1.1
autovacuum = on
log_autovacuum_min_duration = 0
autovacuum_vacuum_cost_delay = 0
autovacuum_max_workers = 4
autovacuum_naptime = 15
default_text_search_config = 'pg_catalog.english'
shared_preload_libraries = 'pg_stat_statements,pgstattuple,pg_buffercache,postgres_fdw'
至此,postgresql 安装完毕
标签:opt,postgresql,postgres,devel,pgdata,pg,下载安装,log From: https://www.cnblogs.com/sunjiwei/p/18451350