PostgreSQL & PostGIS

安装postgresql

配置好yum源之后,使用yum info postgresql可发现 postgresql的版本为9.2.23,若想安装最新版本,可参考下面操作

https://www.postgresql.org/download/linux/redhat/

http://docs.nextgis.com/docs_ngweb/source/install-centos7.html

http://www.postgresonline.com/journal/archives/362-An-almost-idiots-guide-to-install-PostgreSQL-9.5,-PostGIS-2.2-and-pgRouting-2.1.0-with-Yum.html

sudo yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-3.noarch.rpm

# 查看上述仓库中可用的包
yum list | grep postgresql95
# 安装 PostgreSQL  client server 
sudo yum install postgresql95 postgresql95-server postgresql95-libs 
postgresql95-contrib postgresql95-devel
# 查看帮助
psql --help
rpm -qa | grep postgresql*  # 查看已安装软件
# 初始化数据库,并设置随系统启动
sudo /usr/pgsql-9.5/bin/postgresql95-setup initdb
sudo systemctl start postgresql-9.5.service # service postgresql-9.5 start
sudo systemctl enable postgresql-9.5.service

# 编辑验证参数(此处使用nano编辑器,可以使用 vim)
# psql 进入postgres数据库之后,使用 show hba_file; 查看文件位置
sudo nano /var/lib/pgsql/9.5/data/pg_hba.conf 
sudo vim /var/lib/pgsql/9.5/data/pg_hba.conf 

将ident 修改为 md5

PostgreSQL ident和peer基于操作系统用户的认证 PostgreSQL认证方法

如果是 peer,可能会出现 对等认证失败 的错误

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# 可进一步修改 postgresql.conf 以监听目标地址
su postgres   # 切换到postgres    或   sudo su postgres
psql -U postgres   #登录数据库,默认没有密码
ALTER USER postgres WITH PASSWORD '密码'; #修改密码  sudo passwd postgres
# 或者使用 password 命令
password postgres 
select * from pg_shadow; # 查看数据库信息
q #退出
psql -V
sudo systemctl restart postgresql-9.5.service # 重启服务,或  service postgresql-9.5 restart

# 使用postgres用户新建一个psql数据库用户(ngw_admin)
# du 命令可以查看用户信息
# -P 表示密码,-e表示显示命令。 使用 -s 或 --superuser 赋予超级用户权限
sudo -u postgres createuser ngw_admin -P -e
# 新建数据库(db_ngw),所有者为 ngw_admin
sudo -u postgres createdb -O ngw_admin --encoding=UTF8 db_ngw
postgresql-client libraries and client binaries
postgresql-server core database server
postgresql-contrib additional supplied modules
postgresql-devel libraries and headers for C language development
pgadmin4 pgAdmin 4 graphical administration utility

### 安装postgis

https://postgis.net/install/

The postgis2_95-client contains the PostGIS commandline tools shp2gpsql, pgsql2shp, raster2pgsql that are useful for loading or exporting spatial data.

sudo yum install epel-release # 没有配置epel源的话
sudo yum install postgis2_95 postgis2_95-client # 安装
# 需要 pgRouting 时 yum install pgrouting_95
# 登录进入相应数据库
psql -U ngw_admin -d db_ngw
# 为已有数据库(db_ngw)扩展 postgis 功能
CREATE EXTENSION postgis;
SELECT PostGIS_Full_Version(); # 测试数据库是否包含了postgis的功能

或者直接在shell中执行

sudo psql -u postgres -d db_ngw -c 'CREATE EXTENSION postgis;'
sudo psql -u postgres -d db_ngw -c 
'ALTER TABLE geometry_columns OWNER TO ngw_admin;'
sudo psql -u postgres -d db_ngw -c 
'ALTER TABLE spatial_ref_sys OWNER TO ngw_admin;'
sudo psql -u postgres -d db_ngw -c 
'ALTER TABLE geography_columns OWNER TO ngw_admin;'
psql -h localhost -d db_ngw -U ngw_admin -c "SELECT PostGIS_Full_Version();"

使用template方式直接创建postgis数据库,并指定所有者

# 登录数据库
# postgis_21_sample 为 2.1 版本postgis数据库
create  database  geodataont   template   postgis_21_sample   owner   gdo;
# 或者
createdb -O gdo -U postgres -T postgis_22_sample geodataont

启用 PostGIS 功能的相关SQL

DO NOT INSTALL it in the database called postgres.

# 连接数据库之后,执行以下sql命令启用相应功能
# Enable PostGIS (includes raster)
CREATE EXTENSION postgis;
# Enable Topology
CREATE EXTENSION postgis_topology;
# Enable PostGIS Advanced 3D
# and other geoprocessing algorithms
# sfcgal not available with all distributions
CREATE EXTENSION postgis_sfcgal;
# fuzzy matching needed for Tiger
CREATE EXTENSION fuzzystrmatch;
# rule based standardizer
CREATE EXTENSION address_standardizer;
# example rule data set
CREATE EXTENSION address_standardizer_data_us;
# Enable US Tiger Geocoder
CREATE EXTENSION postgis_tiger_geocoder;

CREATE EXTENSION pgrouting;
SELECT * FROM pgr_version();
# yum install ogr_fdw95
CREATE EXTENSION ogr_fdw;

CentOS 7 源码安装PostGIS

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!