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=osspmake && make install# 添加环境变量vim /etc/profilePGDATA=/data/pgsql/11/dataPATH=/usr/local/postgresql/11/bin:$PATHexport PGDATA PATHsource /etc/profile
安装中文分词器
# 安装SCWSwget http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2tar -xf scws-1.2.3.tar.bz2cd scws-1.2.3 ; ./configure ; make install# 安装zhparsergit clone https://github.com/amutu/zhparser.gitcd zhparsermake && make install# 提示pg_config找不到export PG_CONFIG=/usr/local/postgresql/11/bin/pg_config
安装PG插件
# btree_gistpostgresql-11.3/contrib cd btree_gistmake && make install# pg_trgmpostgresql-11.3/contribcd ../pg_trgmmake && make install# dblinkpostgresql-11.3/contrib cd ../dblinkmake && make install# uuid-ossppostgresql-11.3/contrib cd ../uuid-osspmake && make install# pg_stat_statements(监控需要)postgresql-11.3/contrib cd ../pg_stat_statementsmake && make install
安装citus扩展—先不安装
wget https://github.com/citusdata/citus/releases./configuremake && make install# 说明# 编译postgreql后,设置pg_config变量,但是在编译citus时,需要设置软链ln -s /usr/local/pgsql/bin/pg_config /usr/bin/pg_config
安装Postgis
# 安装Proj4wget http://download.osgeo.org/proj/proj-4.9.3.tar.gztar -xf proj-4.9.3.tar.gzcd proj-4.9.3./configure --prefix=/usr/local/postgresql/11/plugin/projmake && make installecho "/usr/local/postgresql/11/plugin/proj/lib" > /etc/ld.so.conf.d/proj-4.9.3.confldconfig# 安装GEOSwget http://download.osgeo.org/geos/geos-3.6.1.tar.bz2tar -jxf geos-3.6.1.tar.bz2cd geos-3.6.1./configure --prefix=/usr/local/postgresql/11/plugin/geosmake && make installecho "/usr/local/postgresql/11/plugin/geos/lib" > /etc/ld.so.conf.d/geos-3.6.1.confldconfig# 安装GDAL 这个安装太慢了wget http://download.osgeo.org/gdal/2.1.2/gdal-2.1.2.tar.gztar -xf gdal-2.1.2.tar.gzcd gdal-2.1.2./configure --prefix=/usr/local/postgresql/11/plugin/gdalmake && make installecho "/usr/local/postgresql/11/plugin/gdal/lib" > /etc/ld.so.conf.d/gdal-2.1.2.confldconfig# 安装PostGISwget https://download.osgeo.org/postgis/source/postgis-2.5.2.tar.gztar -xvzf postgis-2.5.2.tar.gzcd 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/projmake && make install
PG初始化
# postgres运行用户useradd -s /bin/bash -m postgres# 数据目录、流复制归档目录mkdir -p /data/pgsql/11/datamkdir -p /data/pgsql/archivedirmkdir -p /var/log/postgreschown -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 %iAssertPathExists=/etc/postgresql/%I/postgresql.confRequiresMountsFor=/etc/postgresql/%I /var/lib/postgresql/%IPartOf=postgresql.serviceReloadPropagatedFrom=postgresql.serviceBefore=postgresql.service# stop server before networking goes down on shutdownAfter=network.target[Service]Type=forking# -: ignore startup failure (recovery might take arbitrarily long)# the actual pg_ctl timeout is configured in pg_ctl.confExecStart=-/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i start# 0 is the same as infinity, but "infinity" needs systemd 229TimeoutStartSec=0ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast %i stopTimeoutStopSec=1hExecReload=/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i reloadPIDFile=/run/postgresql/%i.pidSyslogIdentifier=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.servicesystemctl status postgresql.servicesystemctl stop postgresql.servicesystemctl disable postgresql.servicesystemctl enable postgresql.service