PostgreSQL 的编译安装及配置

看了网上很多安装PostgreSQL的文档,大多数都是东拼西凑,不够认真。现在我整理了一下我个人比较推崇的做法,个人认为这样安装比较好。

系统:CentOS6.7

PostgreSQL版本:9.4.6

首先准备编译安装所需要的环境。

安装所需要的软件包”

yum -y install lrasz sysstat e4fsprogs ntp readline-devel zlib zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel python-devel tcl-devel gcc make smartmontools flex bison perl perl-devel perl-ExtUtils* openIPMI-tools systemtap-sdt-devel openldap openldap-devel

修改yum源,安装调试工具(根据需求安装)

vim /etc/yum.repos.d/CentOS/Debuginfo.repo
enabled=1
yum install -y kernel-devel-`uname -r` kernel-debuginfo-`uname -r` kernel-debuginfo-common-x86_64_`uname -r` systemtap

修改内核参数

使用vim打开内核参数的配置文件

vim /etc/sysctl.conf
写入以下内容

kernel.shmmni = 4096
kernel.sem = 50100 64128000 50100 1280
fs.file-max = 7672460
net.ipv4.ip_local_port_range = 9000 65000
net.core.rmem_default = 1048576
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_syn_backlog = 4096
net.core.netdev_max_backlog = 10000
vm.overcommit_memory = 0
fs.aio-max-nr = 1048576
net.ipv4.tcp_timestamps = 0

使修改的内核参数生效:

sysctl -p

修改系统的资源限制文件

vim /etc/security/limits.conf

* soft nofile 300000
* hard nofile 300000
* soft nproc 102400
* hard nproc 102400
* soft core unlimited
* hard core unlimited
* soft memlock unlimited
* hard memlock unlimited

关闭selinux

vim /etc/sysconfig/selinux

SELINUX=disabled
setenforce 0

添加postgres用户

useradd postgres

修改postgres用户的环境变量

修改 bash_profile:
vim /home/postgres/.bash_profile

把一下内容写入到 bash_profile:

export PGPORT=5432
export PGDATA=/home/postgres/pgdata/data
export LANG=zh_CN.UTF-8
export PGHOME=/opt/pgsql
export LD_LIBRARY_PATH=PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=PGHOME/bin:PATH:.
export MANPATH=PGHOME/share/man:MANPATH
export PGUSER=postgres
export PGHOST=127.0.0.1
export PGDATABASE=postgres

编译安装PostgreSQL

tar xf postgresql-9.4.6.tar.xz
tar xf pgfince.tar.xz
mv pgfince postgres-9.4.6/contrib
./configure --prefix=/opt/pgsql9.4.6 --with-port=5432 --with-perl --with-tcl --with-python --with-python3 --with-openssl --with-pam --with-ldap --with-libxml --with-libxslt --enable-thread-safety --with-wal-blocksize=8 --with-blocksize=8 && gmake world

以下编译选项酌情添加

--enable-dtrace --enable-debug --enable-cassert
gmake install-world
ln -s /opt/pgsql9.4.6 /opt/pgsql

安装pgfince扩展

cd postgres-9.4.6/contrib/pgfince
. /home/postgres/.bash_profile
make clean
make
make install

初始化数据库

su - postgres
initdb -D $PGDATA -E UTF8 --local=C -U postgres -W

修改数据库配置文件

cd $PGDATA
vim postgresql.conf

启动数据库(大功告成)

pg_ctl start

修改时间:
2019.09.29

发表评论

电子邮件地址不会被公开。 必填项已用*标注