Postgresql数据库的安装
仓库安装
仓库安装可以参考官方的download页面。在这里可以选择操作系统,需要安装的Postgresql版本号,快捷的进行安装和部署。
如果需要离线安装,那么推荐首先进行创建离线仓库,包括base,update,epel,pgdg等等的分类仓库创建,方便日后升级和扩展。如果是容器部署可以参考Postgres offical image。
大型项目除了数据库本身,还需要安装数据库周边的一些软件,依赖复杂度高,所以推荐仓库安装。
源码编译安装
然而有些时候,你不得不进行源码编译安装。因为有些老旧系统和老旧版本,他们的仓库要么关闭,要么停止维护,而生产要求指定Postgresql数据库版本,那么就需要编译安装了。可以在Postgresql source页面中找到对应的版本并下载进行编译安装。
如果是生产环境推荐仓库安装,如果是以学习和调试为目的,那么编译安装是更好的选择。下面以centos7.5上编译安装pg14举例。
安装gcc,离线yum源安装
yum install gcc.x86_64 gcc-c++.x86_64 -y安装依赖
# yum install pyOpenSSL.x86_64 openssl.x86_64 openssl-libs.x86_64 openssl-devel.x86_64 readline-devel zlib-devel systemd-devel uuid-devel.x86_64 tcl-devel.x86_64 tk-devel.x86_64 krb5-devel.x86_64 libicu-devel.x86_64 pam-devel perl-LDAP.noarch perl-Mozilla-LDAP.x86_64 openldap-devel.x86_64 libxml2-devel.x86_64 libxslt-devel.x86_64 perl-ExtUtils-Embed python python-devel源码编译安装
cd /app/package/pg1411/postgresql-14.1 ./configure --prefix=/usr/pgsql-14\ --with-systemd\ --with-uuid=ossp\ --with-pgport=15432\ --with-segsize=2\ --with-blocksize=8\ --with-wal-blocksize=8\ --with-perl\ --with-python\ --with-tcl\ --with-gssapi\ --with-icu\ --with-openssl\ --with-pam\ --with-ldap\ --with-libedit-preferred\ --with-libxml\ --with-libxsltmake world make install-world如果想要重清除主机上的postgres服务,可以参考以下步骤
userdel -fr postgres make uninstall make clean make distclean创建postgres用户
# 创建用户,确定权限及组 groupdel postgres groupadd -g 26 postgres userdel -fr postgres mkdir -p /var/lib/pgsql/14 useradd -u 26 -g postgres -G wheel,root -k /etc/skel -r -s /bin/bash -d /var/lib/pgsql -m postgres [root@localhost ~]# id postgres uid=26(postgres) gid=26(postgres) groups=26(postgres),0(root),10(wheel) # 变更密码 passwd postgres # 路径赋权 [root@localhost ~]# chown -R postgres:postgres /var/lib/pgsql/pg环境变量设置
# vim .bash_profile [ -f /etc/profile ] && source /etc/profile [ -f /var/lib/pgsql/.pgsql_profile ] && source /var/lib/pgsql/.pgsql_profile PGDATA=/pgdata/pg/pg14 # path setting PATH=/usr/pgsql-14/bin/:$PATH export PGLOG=/pgdata/pg_log export PGXLOG=/pgdata/pg_wal创建本地操作记录,加载环境设置变量
touch .bash_history touch .psql_history source .bash_profile安装实例
# 创建实例路径 chmod 700 /pgdata/pg/pg14 chown -R postgres:postgres /pgdata # 创建实例 initdb -D $PGDATA # 启动实例 pg_ctl -D /pgdata/pg/pg14 -l logfile startsystemd注册postgresql-14服务
vim /usr/lib/systemd/system/postgresql-14.service [Unit] Description=PostgreSQL 14 database server Documentation=https://www.postgresql.org/docs/14/static/ After=syslog.target After=network-online.target [Service] Type=notify User=postgres Group=postgres # Note: avoid inserting whitespace in these Environment= lines, or you may # break postgresql-setup. # Location of database directory Environment=PGDATA=/pgdata/pg/pg14 # 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 postmaster OOMScoreAdjust=-1000 Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj Environment=PG_OOM_ADJUST_VALUE=0 ExecStart=/usr/pgsql-14/bin/postmaster -D ${PGDATA} ExecReload=/bin/kill -HUP $MAINPID KillMode=mixed KillSignal=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 229 TimeoutStartSec=0 TimeoutStopSec=1h [Install] WantedBy=multi-user.target重载并重启
systemctl daemon-reload kill $(pidof postgres) systemctl enable postgresql-14 systemctl start postgresql-14 systemctl status postgresql-14
实例配置
操作系统的内核参数配置可以参考德哥的博客
如何度量 Kernel Resources for PostgreSQL
PostgreSQL on Linux 最佳部署手册 - 珍藏级
Postgresql数据库实例中的postgresql.conf负责该实例的参数配置,在官方的Chapter 20. Server Configuration中有详细的参数配置介绍。如果想省事,那么可以参考下面两个网站进行pg的实例参数配置。
需要注意的是,调参并不是一簇而就的事情,在真正投入生产前,你可能需要进行压测,工具则是[pgbench]。在调参前,我还是建议阅读官方文档的参数介绍,结合Postgresql架构及硬件架构,进行先粗后细的调参。
pg_hba.conf文件是另外一个比较重要的配置文件,你可以把它当成pg实例的防火墙配置文件,这个文件涉及到用户登录及数据库安全,总的原则是超管不允许远程连接,权限越大,地址限制越收紧。