PostgreSQL(后面简称pgsql),常用的也就三种安装方式,第一种为yum源直接下载安装,第二种是二进制包安装,第三种源码编译安装,官方建议,在生产环境中,安装部署最好使用源码的方式进行安装,可以更好的自定义一些参数和配置等。
官方源码包下载地址:https://www.postgresql.org/ftp/source/
环境初始化
1.下载安装包,并安装相关依赖
root # cd /opt
root # wget https://ftp.postgresql.org/pub/source/v14.13/postgresql-14.13.tar.gz
root # 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
2.创建postgres用户和目录,后期维护和登录pgsql都需要在postgres用户下进行
root # groupadd postgres
root # useradd -g postgres postgres
root # mkdir /data/pgsql/base -p
root # mkdir /data/pgsql/data -p
编译安装pgsql
1.解压安装包,编译安装pgsql
root # cd /opt
root # tar xf postgresql-14.13.tar.gz
root # cd postgresql-14.13/
root # ./configure --prefix=/data/pgsql/base
root # make && make install
2.对pgsql目录进行授权
root # chown postgres:postgres /data/pgsql/ -R
3.配置postgres用户的环境变量
root # cd /home/postgres/
root # vim .bash_profile
...
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export PGHOME=/data/pgsql/base
export PGDATA=/data/pgsql/data
export PATH=$PATH:$HOME/bin:$PGHOME/bin
初始化pgsql实例
1.切换到postgres用户下执行初始化数据库实例操作
root # su - postgres
postgres # initdb
编辑pgsql相关配置
1.修改pgsql启动文件,postgresql.conf
psotgres # cd /data/pgsql/data/
psotgres # vim postgresql.conf
...
# Do not edit this file manually!
# It will be overwritten by Patroni!
listen_addresses = '*'
port = '5432'
data_directory = '/data/pgsql/data'
hba_file = '/data/pgsql/data/pg_hba.conf'
log_destination = 'stderr'
logging_collector = no
log_directory = '/data/pgsql/data'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_line_prefix = '%t-%d-%h-%a :'
shared_buffers = 128MB
max_connections = '100'
2.配置pgsql访问控制文件,pg_hba.conf。
psotgres # cd /data/pgsql/data/
psotgres # vim pg_hba.conf
...
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 0.0.0.0/0 trust
...
配置pgsql启动脚本
1.从pgsql安装包中,copy启动脚本到/etc/init.d目录。
root # cd /opt/postgresql-14.13/contrib/start-scripts
root # chmod +x linux
root # cp linux /etc/init.d/postgresql
2.编辑pgsql启动脚本
root # vim /etc/init.d/postgresql
...
# Installation prefix
prefix=/data/pgsql/base
# Data directory
PGDATA="/data/pgsql/data"
# Who to run the postmaster as, usually "postgres". (NOT "root")
PGUSER=postgres
# Where to keep a log file
PGLOG="$PGDATA/serverlog"
...
3.将pgsql启动脚本,加入service服务,管理postgresql启动
root # chkconfig --add postgresql
启动pgsql
1.pgsql启动方式,因为前面执行了chkconfig --add,所以systemctl也可以启停pgsql。下面两种方式选择一种执行,启动即可。
#启动方式1:
root # service postgresql start
#启动方式2:
root # systemctl start postgresql
2.连接测试
root # su - postgres
postgres # psql
psql (14.13)
Type "help" for help.
postgres=#