Centos 7上如何安装Postgresql10.5和PostGIS

47次阅读
没有评论

共计 2582 个字符,预计需要花费 7 分钟才能阅读完成。

这篇文章给大家分享的是有关 Centos 7 上如何安装 Postgresql10.5 和 PostGIS 的内容。丸趣 TV 小编觉得挺实用的,因此分享给大家做个参考,一起跟随丸趣 TV 小编过来看看吧。

设置 /etc/resolv.conf

让 linux server 可以上网

查看 postgresql 源:

yum
| grep postgresql

首先安装 PostgreSQL 的 rpm:

yum install
https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos1-10-2.noarch.rpm -y

yum list | grep postgresql

安装 postgresql10-contrib 和 postgresql10-server。

yum install postgresql10-contrib postgresql10-server -y

这样会给我们的系统增加一个 postgres 用户。

cat /etc/passwd

修改默认数据目录

Postgresql 默认的数据目录是 /var/lib/pgsql/ 版本号 /data 目录,这要求你在 /var 下有足够的存储空间,我们这里将其换掉,假设 /home 的空间很大。

首先在 /home 下创建一个 Postgresql 的数据目录,指定所有者 postgres 同时分配权限

mkdir
/home/postgresql_data

chown
postgres:postgres /home/postgresql_data

chmod
/home/postgresql_data

设置环境变量:

export
PATH=/usr/pgsql-10/bin:$PATH

export
LD_LIBRARY_PATH=/usr/pgsql-10/lib

export
PGDATA=/home/postgresql_data

切换到 postgres 用户,使用 initdb 初始化数据库,这样在 /home/postgresql_data 下会增加很多东西。

修改 /usr/lib/systemd/system/postgresql-10.service 文件的内容,在 #Location of database direcotry 里面指定正确的 PGDATA:

#Location of database directoryEnvironment=PGDATA=/home/postgresql_data

配置数据库服务开机启动并立即启动数据库服务:

systemctl enable postgresql-10.service 

service postgresql-10
start

service postgresql-10
status

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

修改密码:

passwd postgres

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

\l 列出当前库: 

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

安装 PostGIS:

先安装几个工具包

yum  install wget net-tools
epel-release
-y

然后安装 postgis

[root@td-db-t01 ~]# yum install postgis24_10 postgis24_10-client -y

yum install postgis24_10 postgis24_10-client
-y

安装拓展工具

yum
install
ogr_fdw10 -y
install
pgrouting_10 -y

创建数据库 spatial_testdb

CREATE
DATABASE
spatial_testdb OWNER postgres;

进入

\c
spatial_testdb

安装 PostGis 扩展

spatial_testdb=#
CREATE
EXTENSION postgis;

spatial_testdb=#
CREATE
EXTENSION postgis_topology;

spatial_testdb=#
CREATE
EXTENSION ogr_fdw;

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

然后可以验证是否安装成功

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

创建空间数据表

存储城市信息(cities),并添加一个存储空间位置的列

spatial_testdb=#
CREATE
TABLE
cities(id
varchar(20),name
varchar(50));

spatial_testdb=#
SELECT
AddGeometryColumn (cities ,
the_geom ,
4326,
POINT ,
2);

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

查询

spatial_testdb=#
SELECT
cities;

spatial_testdb=#
SELECT
id, ST_AsText(the_geom), ST_AsEwkt(the_geom), ST_X(the_geom), ST_Y(the_geom)
cities;

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

空间查询城市相互距离

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

设置远程连接

修改配置文件

首先修改 /home/postgresql_data/pg_hba.conf,改为: 

原先是:

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

改为:

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

其次修改 /home/postgresql_data/postgresql.conf,改为: 

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

改为:

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

改为:

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

之后重启服务

service postgresql-10 restart

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

重要:开启服务器防火墙

firewall-cmd
–add-service=postgresql
–permanent
  开放 postgresql 服务

firewall-cmd
–reload
  重载防火墙

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

远程连接  

这里使用 pgAdmin 进行远程连接,下载地址:https://www.pgadmin.org/download/pgadmin-4-windows/。选择创建服务器,填入相应内容,主机名称填自己服务器的 IP 。

Centos 7 上如何安装 Postgresql10.5 和 PostGIS

如果你的系统上没有安装使用命令安装

安装 firewalld 防火墙 yum install firewalld

开启服务 systemctl start firewalld.service

关闭防火墙 systemctl stop firewalld.service

开机自动启动 systemctl enable firewalld.service

关闭开机制动启动 systemctl disable firewalld.service

感谢各位的阅读!关于“Centos 7 上如何安装 Postgresql10.5 和 PostGIS”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-24发表,共计2582字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)