PostgreSQL部署手册
安装依赖包
apt-get install -y make gcc g++
apt-get install -y libreadline-dev zlib1g-dev uuid uuid-dev libxml2-dev libgdal-dev libossp-uuid-dev
安装PostgreSQL
# 源码安装
wget https://ftp.postgresql.org/pub/source/v11.3/postgresql-11.3.tar.gz
./configure --prefix=/usr/local/postgresql/11 --with-uuid=ossp
make && make install
# 添加环境变量
vim /etc/profile
PGDATA=/data/pgsql/11/data
PATH=/usr/local/postgresql/11/bin:$PATH
export PGDATA PATH
source /etc/profile
安装中文分词器
# 安装SCWS
wget http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2
tar -xf scws-1.2.3.tar.bz2
cd scws-1.2.3 ; ./configure ; make install
# 安装zhparser
git clone https://github.com/amutu/zhparser.git
cd zhparser
make && make install
# 提示pg_config找不到
export PG_CONFIG=/usr/local/postgresql/11/bin/pg_config
安装PG插件
# btree_gist
postgresql-11.3/contrib
cd btree_gist
make && make install
# pg_trgm
postgresql-11.3/contrib
cd ../pg_trgm
make && make install
# dblink
postgresql-11.3/contrib
cd ../dblink
make && make install
# uuid-ossp
postgresql-11.3/contrib
cd ../uuid-ossp
make && make install
# pg_stat_statements(监控需要)
postgresql-11.3/contrib
cd ../pg_stat_statements
make && make install
安装citus扩展—先不安装
wget https://github.com/citusdata/citus/releases
./configure
make && make install
# 说明
# 编译postgreql后,设置pg_config变量,但是在编译citus时,需要设置软链
ln -s /usr/local/pgsql/bin/pg_config /usr/bin/pg_config
安装Postgis
# 安装Proj4
wget http://download.osgeo.org/proj/proj-4.9.3.tar.gz
tar -xf proj-4.9.3.tar.gz
cd proj-4.9.3
./configure --prefix=/usr/local/postgresql/11/plugin/proj
make && make install
echo "/usr/local/postgresql/11/plugin/proj/lib" > /etc/ld.so.conf.d/proj-4.9.3.conf
ldconfig
# 安装GEOS
wget http://download.osgeo.org/geos/geos-3.6.1.tar.bz2
tar -jxf geos-3.6.1.tar.bz2
cd geos-3.6.1
./configure --prefix=/usr/local/postgresql/11/plugin/geos
make && make install
echo "/usr/local/postgresql/11/plugin/geos/lib" > /etc/ld.so.conf.d/geos-3.6.1.conf
ldconfig
# 安装GDAL 这个安装太慢了
wget http://download.osgeo.org/gdal/2.1.2/gdal-2.1.2.tar.gz
tar -xf gdal-2.1.2.tar.gz
cd gdal-2.1.2
./configure --prefix=/usr/local/postgresql/11/plugin/gdal
make && make install
echo "/usr/local/postgresql/11/plugin/gdal/lib" > /etc/ld.so.conf.d/gdal-2.1.2.conf
ldconfig
# 安装PostGIS
wget https://download.osgeo.org/postgis/source/postgis-2.5.2.tar.gz
tar -xvzf postgis-2.5.2.tar.gz
cd postgis-2.5.2
./configure --prefix=/usr/local/postgresql/11/plugin/postgis \
--with-pgconfig=/usr/local/postgresql/11/bin/pg_config \
--with-geosconfig=/usr/local/postgresql/11/plugin/geos/bin/geos-config \
--with-gdalconfig=/usr/local/postgresql/11/plugin/gdal/bin/gdal-config \
--with-projdir=/usr/local/postgresql/11/plugin/proj
make && make install
PG初始化
# postgres运行用户
useradd -s /bin/bash -m postgres
# 数据目录、流复制归档目录
mkdir -p /data/pgsql/11/data
mkdir -p /data/pgsql/archivedir
mkdir -p /var/log/postgres
chown -R postgres.postgres /data/pgsql /var/log/postgres
# 初始化
su - postgres
/usr/local/postgresql/11/bin/initdb -D /data/pgsql/11/data
# 启动
/usr/local/postgresql/11/bin/pg_ctl -D /data/pgsql/11/data -l /var/log/postgres/postgresql-11-main.log start
使用systemctl管理pg
vi /etc/systemd/system/postgresql@11-main.service
# systemd service template for PostgreSQL clusters. The actual instances will
# be called "postgresql@version-cluster", e.g. "postgresql@9.3-main". The
# variable %i expands to "version-cluster", %I expands to "version/cluster".
# (%I breaks for cluster names containing dashes.)
[Unit]
Description=PostgreSQL Cluster %i
AssertPathExists=/etc/postgresql/%I/postgresql.conf
RequiresMountsFor=/etc/postgresql/%I /var/lib/postgresql/%I
PartOf=postgresql.service
ReloadPropagatedFrom=postgresql.service
Before=postgresql.service
# stop server before networking goes down on shutdown
After=network.target
[Service]
Type=forking
# -: ignore startup failure (recovery might take arbitrarily long)
# the actual pg_ctl timeout is configured in pg_ctl.conf
ExecStart=-/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i start
# 0 is the same as infinity, but "infinity" needs systemd 229
TimeoutStartSec=0
ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast %i stop
TimeoutStopSec=1h
ExecReload=/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i reload
PIDFile=/run/postgresql/%i.pid
SyslogIdentifier=postgresql@%i
# prevent OOM killer from choosing the postmaster (individual backends will
# reset the score to 0)
OOMScoreAdjust=-900
# restarting automatically will prevent "pg_ctlcluster ... stop" from working,
# so we disable it here. Also, the postmaster will restart by itself on most
# problems anyway, so it is questionable if one wants to enable external
# automatic restarts.
#Restart=on-failure
# (This should make pg_ctlcluster stop work, but doesn't:)
#RestartPreventExitStatus=SIGINT SIGTERM
[Install]
WantedBy=multi-user.target
systemctl start postgresql.service
systemctl status postgresql.service
systemctl stop postgresql.service
systemctl disable postgresql.service
systemctl enable postgresql.service