blog/linux/centos安装使用postgress

centos安装使用postgress

# 官网下载
wget https://ftp.postgresql.org/pub/source/v10.1/postgresql-10.1.tar.gz

# 自己的镜像下载
wget http://tar.matosiki.site/tar/postgresql-10.1.tar.gz

# 安装c语言环境
yum -y install gcc


yum -y install gcc-c++

yum -y install readline-devel zlib zlib-devel uuid uuid-devel readline-devel

编译

mkdir -p /usr/local/pgsql
tar -zxvf postgresql-10.1.tar.gz -C /usr/local/pgsql
cd /usr/local/pgsql/
mv postgresql-10.1/* /usr/local/pgsql/

./configure LDFLAGS=-L/usr/local/lib CPPFLAGS=-I/usr/local/include --prefix=/usr/local/postgres --with-ossp-uuid

make

make install

添加用户

adduser postgres

# 设置密码为postgres
passwd postgres

mkdir -p /data/postgres

chown -R postgres /data/postgres

chmod -R 700 /data/postgres

安装插件

cd /usr/local/pgsql/contrib/adminpack
make
make install

cd /usr/local/pgsql/contrib/uuid-ossp
make
make install

# 为`libuuid.so.16`建立链接
ln -s /usr/local/lib/libuuid.so.16 /usr/lib64

完成后检查一下权限,如果出现无法找到该库文件的错误,就是由于该文件的权限引起

初始化数据库

su postgres
cd /usr/local/postgres/bin
./initdb -D /data/postgres

配置数据库

vim /data/postgres/postgresql.conf
# 更改内容
listen_addresses = '*'
vim /data/postgres/pg_hba.conf
# 最下边添加一行
host all all 0.0.0.0/0 md5

启动服务

/usr/local/postgres/bin/postgres -D /data/postgres

此时shell被独占,需要重新开启一个shell并切换到postgres账号进行后续操作
进入postgres

cd /usr/local/postgres/bin
su postgres
./psql postgres

\password postgres
# 输入新密码

# 创建uuid扩展
create extension "uuid-ossp";

# 退出终端
\q

使用root权限 开启5432端口

firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload

设置服务自启动

创建文件

vim /usr/lib/systemd/system/postgres.service

内容

[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
Type=forking
User=postgres
Group=postgres
PIDFile=/data/postgres/postmaster.pid

OOMScoreAdjust=-1000
Environment=PGSTARTTIMEOUT=270
Environment=PGDATA=/data/postgres


ExecStart=/usr/local/postgres/bin/pg_ctl start -D ${PGDATA} -s -w -t ${PGSTARTTIMEOUT}
ExecStop=/usr/local/postgres/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/local/postgres/bin/pg_ctl reload -D ${PGDATA} -s

TimeoutSec=300

[Install]
WantedBy=multi-user.target

安装 PostGIs 扩展

安装其它依赖

yum install -y libxml2 libxml2-devel json-c json-c-devel  sqlite-devel

编译安装 proj

下载地址:http://download.osgeo.org/proj/
官网5.2.0下载地址 https://download.osgeo.org/proj/proj-5.2.0.tar.gz

cd ~
wget http://tar.matosiki.site/tar/proj-5.2.0.tar.gz
tar -zxvf proj-5.2.0.tar.gz
cd proj-5.2.0
./configure --prefix=/usr/local/proj
make && make install

编译安装geos

下载地址:http://trac.osgeo.org/geos/
官方3.7.1地址 http://download.osgeo.org/geos/geos-3.7.1.tar.bz2

wget http://tar.matosiki.site/tar/geos-3.7.1.tar.bz2
bzip2 -d geos-3.7.1.tar.bz2
tar vxf geos-3.7.1.tar
cd geos-3.7.1
./configure --prefix=/usr/local/geos
make && make install

编译安装gdal

下载地址:http://download.osgeo.org/gdal/
官网2.4下载地 http://download.osgeo.org/gdal/2.4.0/gdal-2.4.0.tar.gz

tar -zxvf gdal-2.4.0.tar.gz
wget http://tar.matosiki.site/tar/gdal-2.4.0.tar.gz
cd gdal-2.4.0
./configure --prefix=/usr/local/gdal
make && make install

编译postgis
https://download.osgeo.org/postgis/source/postgis-2.5.1.tar.gz

wget http://tar.matosiki.site/tar/postgis-2.5.1.tar.gz
tar -zxvf postgis-2.5.1.tar.gz
cd postgis-2.5.1
./configure --with-pgconfig=/usr/local/postgres/bin/pg_config --with-geosconfig=/usr/local/geos/bin/geos-config --with-projdir=/usr/local/proj --with-gdalconfig=/usr/local/gdal/bin/gdal-config --with-jsondir=/usr/include/json-c
make & make install

postgresql源码目录的contrib/fuzzystrmatch下直接make & make install安装该插件

重启数据库,执行以下查询验证插件是否安装成功

/usr/local/pgsql/contrib/fuzzystrmatch
./configure --with-pgconfig=/usr/local/postgres/bin/pg_config --with-geosconfig=/usr/local/geos/bin/geos-config --with-projdir=/usr/local/proj --with-gdalconfig=/usr/local/gdal/bin/gdal-config --with-jsondir=/usr/include/json-c
make & make install
select \* from pg_available_extensions where name like 'postgis%';

navicat的查询分析器里执行

CREATE EXTENSION fuzzystrmatch;

解决依赖问题

vim /etc/ld.so.conf.d/postgis.conf

写入以下内容

/usr/local/gdal/lib/
/usr/local/proj/lib/
/usr/local/geos/lib/

执行ldconfig命令使配置生效

ldconfig
su postgres
cd /usr/local/postgres/bin

创建一个用于演示的空间模板数据库

./createdb template_postgis

postgis库赋予空间扩展

./psql -f /usr/local/postgres/share/contrib/postgis-2.5/postgis.sql -d template_postgis

./psql -f /usr/local/postgres/share/contrib/postgis-2.5/spatial_ref_sys.sql -d template_postgis

该库不要做任何改动,仅做为库的模板使用,以后要新建有 PostGIS 功能的库就以此为模板即可

库中的spatial_ref_sys表也不要改动或删除

安装PostGIS的座标转换扩展,支持以下座标类型转换

  • BD2GCJ:百度坐标转火星坐标
  • GCJ2BD:火星坐标转百度坐标
  • WGS2GCJ:84(gps)坐标转火星,常用把自己的数据加偏移叠加到高德,谷歌,esri 底图上可用
  • GCJ2WGS:火星转 84
  • BD2WGS:百度转 84
  • WGS2BD:84 转百度

扩展下载地址:https://github.com/FreeGIS/postgis_LayerTransform

将库中的sql文件里的sql语句在template_postgis上执行一次即可。

本文总阅读量