PostgreSQL部署手册

安装依赖包

  1. apt-get install -y make gcc g++
  2. apt-get install -y libreadline-dev zlib1g-dev uuid uuid-dev libxml2-dev libgdal-dev libossp-uuid-dev

安装PostgreSQL

  1. # 源码安装
  2. wget https://ftp.postgresql.org/pub/source/v11.3/postgresql-11.3.tar.gz
  3. ./configure --prefix=/usr/local/postgresql/11 --with-uuid=ossp
  4. make && make install
  5. # 添加环境变量
  6. vim /etc/profile
  7. PGDATA=/data/pgsql/11/data
  8. PATH=/usr/local/postgresql/11/bin:$PATH
  9. export PGDATA PATH
  10. source /etc/profile

安装中文分词器

  1. # 安装SCWS
  2. wget http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2
  3. tar -xf scws-1.2.3.tar.bz2
  4. cd scws-1.2.3 ; ./configure ; make install
  5. # 安装zhparser
  6. git clone https://github.com/amutu/zhparser.git
  7. cd zhparser
  8. make && make install
  9. # 提示pg_config找不到
  10. export PG_CONFIG=/usr/local/postgresql/11/bin/pg_config

安装PG插件

  1. # btree_gist
  2. postgresql-11.3/contrib
  3. cd btree_gist
  4. make && make install
  5. # pg_trgm
  6. postgresql-11.3/contrib
  7. cd ../pg_trgm
  8. make && make install
  9. # dblink
  10. postgresql-11.3/contrib
  11. cd ../dblink
  12. make && make install
  13. # uuid-ossp
  14. postgresql-11.3/contrib
  15. cd ../uuid-ossp
  16. make && make install
  17. # pg_stat_statements(监控需要)
  18. postgresql-11.3/contrib
  19. cd ../pg_stat_statements
  20. make && make install

安装citus扩展—先不安装

  1. wget https://github.com/citusdata/citus/releases
  2. ./configure
  3. make && make install
  4. # 说明
  5. # 编译postgreql后,设置pg_config变量,但是在编译citus时,需要设置软链
  6. ln -s /usr/local/pgsql/bin/pg_config /usr/bin/pg_config

安装Postgis

  1. # 安装Proj4
  2. wget http://download.osgeo.org/proj/proj-4.9.3.tar.gz
  3. tar -xf proj-4.9.3.tar.gz
  4. cd proj-4.9.3
  5. ./configure --prefix=/usr/local/postgresql/11/plugin/proj
  6. make && make install
  7. echo "/usr/local/postgresql/11/plugin/proj/lib" > /etc/ld.so.conf.d/proj-4.9.3.conf
  8. ldconfig
  9. # 安装GEOS
  10. wget http://download.osgeo.org/geos/geos-3.6.1.tar.bz2
  11. tar -jxf geos-3.6.1.tar.bz2
  12. cd geos-3.6.1
  13. ./configure --prefix=/usr/local/postgresql/11/plugin/geos
  14. make && make install
  15. echo "/usr/local/postgresql/11/plugin/geos/lib" > /etc/ld.so.conf.d/geos-3.6.1.conf
  16. ldconfig
  17. # 安装GDAL 这个安装太慢了
  18. wget http://download.osgeo.org/gdal/2.1.2/gdal-2.1.2.tar.gz
  19. tar -xf gdal-2.1.2.tar.gz
  20. cd gdal-2.1.2
  21. ./configure --prefix=/usr/local/postgresql/11/plugin/gdal
  22. make && make install
  23. echo "/usr/local/postgresql/11/plugin/gdal/lib" > /etc/ld.so.conf.d/gdal-2.1.2.conf
  24. ldconfig
  25. # 安装PostGIS
  26. wget https://download.osgeo.org/postgis/source/postgis-2.5.2.tar.gz
  27. tar -xvzf postgis-2.5.2.tar.gz
  28. cd postgis-2.5.2
  29. ./configure --prefix=/usr/local/postgresql/11/plugin/postgis \
  30. --with-pgconfig=/usr/local/postgresql/11/bin/pg_config \
  31. --with-geosconfig=/usr/local/postgresql/11/plugin/geos/bin/geos-config \
  32. --with-gdalconfig=/usr/local/postgresql/11/plugin/gdal/bin/gdal-config \
  33. --with-projdir=/usr/local/postgresql/11/plugin/proj
  34. make && make install

PG初始化

  1. # postgres运行用户
  2. useradd -s /bin/bash -m postgres
  3. # 数据目录、流复制归档目录
  4. mkdir -p /data/pgsql/11/data
  5. mkdir -p /data/pgsql/archivedir
  6. mkdir -p /var/log/postgres
  7. chown -R postgres.postgres /data/pgsql /var/log/postgres
  8. # 初始化
  9. su - postgres
  10. /usr/local/postgresql/11/bin/initdb -D /data/pgsql/11/data
  11. # 启动
  12. /usr/local/postgresql/11/bin/pg_ctl -D /data/pgsql/11/data -l /var/log/postgres/postgresql-11-main.log start

使用systemctl管理pg

  1. vi /etc/systemd/system/postgresql@11-main.service
  2. # systemd service template for PostgreSQL clusters. The actual instances will
  3. # be called "postgresql@version-cluster", e.g. "postgresql@9.3-main". The
  4. # variable %i expands to "version-cluster", %I expands to "version/cluster".
  5. # (%I breaks for cluster names containing dashes.)
  6. [Unit]
  7. Description=PostgreSQL Cluster %i
  8. AssertPathExists=/etc/postgresql/%I/postgresql.conf
  9. RequiresMountsFor=/etc/postgresql/%I /var/lib/postgresql/%I
  10. PartOf=postgresql.service
  11. ReloadPropagatedFrom=postgresql.service
  12. Before=postgresql.service
  13. # stop server before networking goes down on shutdown
  14. After=network.target
  15. [Service]
  16. Type=forking
  17. # -: ignore startup failure (recovery might take arbitrarily long)
  18. # the actual pg_ctl timeout is configured in pg_ctl.conf
  19. ExecStart=-/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i start
  20. # 0 is the same as infinity, but "infinity" needs systemd 229
  21. TimeoutStartSec=0
  22. ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast %i stop
  23. TimeoutStopSec=1h
  24. ExecReload=/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i reload
  25. PIDFile=/run/postgresql/%i.pid
  26. SyslogIdentifier=postgresql@%i
  27. # prevent OOM killer from choosing the postmaster (individual backends will
  28. # reset the score to 0)
  29. OOMScoreAdjust=-900
  30. # restarting automatically will prevent "pg_ctlcluster ... stop" from working,
  31. # so we disable it here. Also, the postmaster will restart by itself on most
  32. # problems anyway, so it is questionable if one wants to enable external
  33. # automatic restarts.
  34. #Restart=on-failure
  35. # (This should make pg_ctlcluster stop work, but doesn't:)
  36. #RestartPreventExitStatus=SIGINT SIGTERM
  37. [Install]
  38. WantedBy=multi-user.target
  1. systemctl start postgresql.service
  2. systemctl status postgresql.service
  3. systemctl stop postgresql.service
  4. systemctl disable postgresql.service
  5. systemctl enable postgresql.service