前言
最近听了公司项目中需要用到PG数据库,然后结合查看了官方文档,将自己安装PG数据库遇到的问题总结起来,供大家一起分享。
1.软件需求
proj-4.9.2.tar.gz
gdal-2.1.1.tar.gz
geos-3.5.0.tar.bz2
postgis-2.3.0.tar.gz
postgresql-10.4.tar.gz
2.创建用户
[root@hg ~]# useradd postgres
[root@hg ~]# groupadd postgres
3.postgresSQL 安装
3.1 安装依赖
#yum -y install gcc ;
#yum install zlib-devel;
#yum install readline;
#yum install readline-devel;
#yum install libxml2-devel.x86_64;
3.2 解压安装
# tar -zxvf postgresql-10.4.tar.gz
# cd postgresql-10.4
#./configure --prefix=/usr/local/pgsql
#make
#make install
3.3 创建初始化目录
#cd /usr/local/pgsql
#mkdir data
#chown postgres.postgres data
3.4 添加环境变量
export POSTGRESQL_HOME=/usr/local/pgsql
export PGDATA=/usr/local/pgsql/data
export PATH=$PATH:$POSTGRESQL_HOME/bin:$PGDATA
export GDAL=/use/local/gdal
export PATH=$PATH:$GDAL/bin
export PROJ_HOME=/usr/local/proj
export GEOS_HOME=/use/local/geos
export LD_LIBRARY_PATH=$POSTGRESQL_HOME/bin:$PROJ_HOME/bin:$GEOS_HOME/bin
3.5 初始化
#su - postgres
#/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
3.6 启动PG数据库
# /usr/local/pgsql/bin/pg_ctl start
3.7 配置文件
目前位置,该数据库只能允许本地访问,如果运行其他用户访问的话还需继续进行如下配置:
# vim data/postgresql.conf
#原配置
# listen_addresses = '127.0.0.1'
# port = 5432
#修改后
# listen_addresses = '*'
# port = 5432
允许远程主机连接
vi data/pg_hba.conf //编辑配置文件 天加如下配置host all all 0.0.0.0/0 password
# IPv4 local connections:
host all all 0.0.0.0/0 password
host all all 127.0.0.1/32 trust
# IPv6 local connections:
#host all all ::1/128 trust
其中,"trust"和"password"为postgresql数据库登陆验证的方式,"trust"表示信任,即不需要输入密码(即使有密码),"password"表示需要输入密码。
host all all 0.0.0.0/0 所有主机连接数据库需要输入密码(如果有密码)
打开日志
vim data/postgresql.conf
log_destination = 'stderr'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_file_mode = 0600
log_rotation_size = 10MB
4.安装扩展postgis
4.1 安装GEOS
wget http://download.osgeo.org/geos/geos-3.5.0.tar.bz2
tar -jxvf geos-3.5.0.tar.bz2
cd geos-3.5.0
./configure --prefix=/usr/local/geos
make
make install
4.2 安装PROJ
wget http://download.osgeo.org/proj/proj-4.9.2.tar.gz
tar -zxvf proj-4.9.2.tar.gz
cd proj-4.9.2
./configure --prefix=/usr/local/proj
make
make install
4.3 安装GDAL
wget http://download.osgeo.org/gdal/2.1.1/gdal-2.1.1.tar.gz
tar -zxvf gdal-2.1.1.tar.gz
cd gdal-2.1.1
./configure --prefix=/opt/gdal --with-pg=/opt/postgresql-9.3.2/bin/pg_config
make
make install
4.4 安装依赖软件
yum install -y libtool libxml2 libxml2-devel libxslt libxslt-devel json-c json-c-devel cmake gmp gmp-devel mpfr mpfr-devel boost-devel pcre-devel
4.5 安装postgis
wget http://download.osgeo.org/postgis/source/postgis-2.3.0.tar.gz
tar -zxvf postgis-2.3.0.tar.gz
./configure --prefix=/usr/local/postgis --with-gdalconfig=/usr/local/bin/gdal-config --with-pgconfig=/usr/local/pgsql/bin/pg_config --with-geosconfig=/usr/local/geos/bin/geos-config --with-projdir=/usr/local/proj
make
make install