温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-11-10 11:17:33 来源:亿速云 阅读:221 作者:小新 栏目:关系型数据库

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

设置/etc/resolv.conf

让linux server可以上网

查看postgresql源:

yum list | grep postgresql

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

首先安装PostgreSQL的rpm:

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

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

yum list | grep postgresql

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

安装postgresql10-contrib和postgresql10-server。

yum install postgresql10-contrib postgresql10-server -y

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

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

cat /etc/passwd

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

修改默认数据目录

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

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

mkdir /home/postgresql_data

chown postgres:postgres /home/postgresql_data

chmod 750 /home/postgresql_data

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

设置环境变量:

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下会增加很多东西。

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

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

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

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

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

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

systemctl enable postgresql-10.service 

service postgresql-10 start

service postgresql-10 status

Centos 7上如何安装Postgresql10.5和PostGISCentos 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 yum 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 * FROM cities;

spatial_testdb=# SELECT id, ST_AsText(the_geom), ST_AsEwkt(the_geom), ST_X(the_geom), ST_Y(the_geom) FROM 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”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI