千家信息网

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

发表于:2025-02-08 作者:千家信息网编辑
千家信息网最后更新 2025年02月08日,这篇文章给大家分享的是有关Centos 7上如何安装Postgresql10.5和PostGIS的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。设置/etc/resolv.c
千家信息网最后更新 2025年02月08日Centos 7上如何安装Postgresql10.5和PostGIS

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

设置/etc/resolv.conf

让linux server可以上网

查看postgresql源:

yum list | 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 750 /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

修改密码:

passwd postgres

\l 列出当前库:

安装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;

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

创建空间数据表

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

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

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

查询

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;

空间查询城市相互距离

设置远程连接

修改配置文件

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

原先是:

改为:

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

改为:

改为:

之后重启服务

service postgresql-10 restart

重要:开启服务器防火墙

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

firewall-cmd --reload 重载防火墙

远程连接

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

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

安装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"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0