乐于分享
好东西不私藏

PostgreSQL 15 部署文档

PostgreSQL 15 部署文档

一、文档介绍

文档介绍 PostgreSQL 15 编译安装操作步骤。 

二、部署说明

  • 操作系统:
  • 安装包版本:

三、安装数据库

(一) 创建用户

useradd postgres

(二) 下载安装包

cd /usr/local/srcwget https://ftp.postgresql.org/pub/source/v15.15/postgresql-15.15.tar.gz

(三) 安装依赖包

1. Centos 7

yum install -y readline-devel zlib-devel openssl-devel libxml2-devel libxslt-devel libuuid-devel icu libicu-devel lz4-devel zstd-devel

2. AlmaLinux 8

yum install -y readline-devel zlib-devel openssl-devel libxml2-devel libxslt-devel libuuid-devel icu libicu-devel lz4-devel zstd-devel

3. Ubuntu 22

apt install -y gcc make libreadline-dev zlib1g-dev libssl-dev libxml2-dev libxslt1-dev libicu-dev uuid-dev liblz4-dev libzstd-dev pkg-config libsystemd-dev libperl-dev libpython3-dev flex bison

(四) 编译

1. centos 7

注意:需要安装更高版本的 openssl。

export CPPFLAGS="-I/usr/local/openssl-1.1.1w/include"export LDFLAGS="-L/usr/local/openssl-1.1.1w/lib"export LD_LIBRARY_PATH="/usr/local/openssl-1.1.1w/lib"./configure \  --prefix=/app/funo/pgsql \  --with-readline \  --with-uuid=e2fs \  --with-openssl \  --with-libxml \  --with-libxslt \  --with-icu \  --with-lz4 \  --with-zstd \  --enable-thread-safety \  --enable-integer-datetimesmake world -j2make install-world

2. 更高版本

cd /usr/local/srctar zxf postgresql-15.15.tar.gzcd postgresql-15.15./configure --prefix=/usr/local/pgsql \    --with-readline \    --with-uuid=e2fs \    --with-openssl \    --with-libxml \    --with-libxslt \    --with-icu \    --with-lz4 \    --with-zstd \    --enable-thread-safety \    --enable-integer-datetimesmake world -j2make install-world

(五) 更新目录权限

mkdir -p /data/pgsql_data/5432/{arvhive,conf,data,tmp}chown -R postgres.postgres /usr/local/pgsql/chown -R postgres.postgres /data/pgsql_data

(六) 配置变量

echo '# postgresexport PGHOME=/usr/local/pgsql/export PGDATA=/data/pgsql_data/5432/dataexport PGUSER=postgresexport PGDATABASE=postgresexport PATH=$PATH:$PGHOME/bin' >>  /etc/profilesource /etc/profile

(七) 初始化数据库

需要进入 postgres 用户下执行:su - postgres

cd $PGDATAinitdb -A md5 -D $PGDATA -E utf8 --locale=C -U postgres -W -k# 输入密码

(八) 更新配置

1. postgresql.conf

修改以下参数

listen_addresses = '*'  max_connections = 3000 #tcp_keepalives_idle = 60 #tcp_keepalives_interval = 30 #tcp_keepalives_count = 5  password_encryption = md5 shared_buffers = 4GB work_mem = 32MB maintenance_work_mem = 512MBdynamic_shared_memory_type = posix wal_level = replica      wal_log_hints = on max_wal_size = 1GBmin_wal_size = 80MBarchive_command = 'cp %p /data/pgsql_data/5432/archive/%f'wal_keep_size = 256  log_timezone = 'Asia/Shanghai'statement_timeout = 5min  idle_in_transaction_session_timeout = 30min datestyle = 'iso, mdy'timezone = 'Asia/Shanghai'lc_messages = 'C'  lc_monetary = 'C'   lc_numeric = 'C'  lc_time = 'C'   default_text_search_config = 'pg_catalog.english'

2. pg_hba.conf

新增以下配置

host    all             all             0.0.0.0/0               md5

注意:根据业务实际情况添加允许访问的源地址

(九) 创建服务

服务文件路径:/usr/lib/systemd/system/postgresql-15.service

# It's not recommended to modify this file in-place, because it will be# overwritten during package upgrades.  It is recommended to use systemd# "dropin" feature;  i.e. create file with suffix .conf under# /etc/systemd/system/postgresql-15.service.d directory overriding the# unit's defaults. You can also use "systemctl edit postgresql-15"# Look at systemd.unit(5) manual page for more info.Note: changing PGDATA will typically require adjusting SELinux# configuration as well.Note: do not use a PGDATA pathname containing spaces, or you will# break postgresql-15-setup.[Unit]Description=PostgreSQL 15 database serverDocumentation=https://www.postgresql.org/docs/15/static/After=syslog.targetAfter=network-online.target[Service]Type=forkingUser=postgresGroup=postgresNote: avoid inserting whitespace in these Environment= lines, or you may# break postgresql-setup.# Location of database directoryEnvironment=PGDATA=/data/pgsql_data/5432/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 postmasterOOMScoreAdjust=-1000Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adjEnvironment=PG_OOM_ADJUST_VALUE=0ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -w -t 300ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fastExecReload=/usr/local/pgsql/bin/pg_ctl reload -D ${PGDATA} KillMode=mixedKillSignal=SIGINT# Do not set any timeout value, so that systemd will not kill postmaster# during crash recovery.TimeoutSec=0# 0 is the same as infinity, but "infinity" needs systemd 229TimeoutStartSec=0TimeoutStopSec=1hLimitNOFILE=655350LimitNPROC=655350LimitAS=infinityLimitFSIZE=infinityLimitMEMLOCK=infinity[Install]WantedBy=multi-user.target

(十) 启动服务

systemctl daemon-reloadsystemctl start postgresql-15.servicesystemctl status postgresql-15.servicesystemctl enable postgresql-15.service