在 CentOS/RHEL 8 上安装 PostgreSQL Yum 存储库:
PostGIS 是 PostgreSQL 的扩展。
因此,我们必须在安装 PostGIS 扩展之前安装 PostgreSQL 数据库。
首先,在 Linux 服务器中添加 PostgreSQL 官方 yum 存储库。
这个 yum 存储库提供了在 Linux 发行版上安装 PostgreSQL 和 PostGIS 所需的所有包。
通过执行以下 dnf 命令安装 PostgreSQL yum 存储库。
# dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
禁用标准 yum 存储库中的内置 PostgreSQL 模块。
# dnf -qy module disable postgresql
现在,我们可以使用 dnf 命令安装我们喜欢的 PostgreSQL 数据库版本。
虽然 PostgreSQL 14 已经发布,但由于 PostGIS 扩展的稳定性和支持,我们使用的是 PostgreSQL 13.
# dnf install -y postgresql13-server
初始化 PostgreSQL 数据库服务器。
# /usr/pgsql-13/bin/postgresql-13-setup initdb Initializing database ... OK
启用并启动 PostgreSQL 数据库服务。
# systemctl enable --now postgresql-13
查看 PostgreSQL 数据库服务的状态,确保其启动成功。
# systemctl status postgresql-13.service
验证 PostgreSQL 数据库的版本。
# psql -V psql (PostgreSQL) 13.4
连接 PostgreSQL 数据库并设置 PostgreSQL 管理员 (postgres) 的密码。
# su - postgres $ psql psql (13.4) Type "help" for help. postgres=# ALTER USER postgres WITH PASSWORD '123'; ALTER ROLE postgres=# \q $ exit logout
默认情况下,PostgreSQL 服务运行在本地接口上,我们可以通过执行以下命令来检查这一点。
# ss -tulpn | grep 5432 tcp LISTEN 0 128 127.0.0.1:5432 0.0.0.0:* users:(("postmaster",pid=2189,fd=7)) tcp LISTEN 0 128 [::1]:5432 [::]:* users:(("postmaster",pid=2189,fd=6))
要使 PostgreSQL 服务可以通过网络访问,我们必须在所有网络接口上运行它。
在 vim 文本编辑器中编辑 PostgreSQL 配置文件。
# vi /var/lib/pgsql/13/data/postgresql.conf
其中找到以下指令。
#listen_addresses = 'localhost'
并将其替换为以下指令。
listen_addresses = '*'
允许网络客户端访问 pg_hba.conf 文件中的 PostgreSQL 服务。
# echo "host all all 192.168.116.0/24 md5" >> /var/lib/pgsql/13/data/pg_hba.conf
重新启动数据库服务使更改生效。
# systemctl restart postgresql-13.service
验证 PostgreSQL 服务,现在它正在侦听所有接口。
# ss -tulpn | grep 5432 tcp LISTEN 0 128 0.0.0.0:5432 0.0.0.0:* users:(("postmaster",pid=2321,fd=6)) tcp LISTEN 0 128 [::]:5432 [::]:* users:(("postmaster",pid=2321,fd=7))
在 Linux 防火墙中允许 PostgreSQL 服务。
# firewall-cmd --permanent --add-service=postgresql success # firewall-cmd --reload success
为 PostgreSQL 配置 PostGIS 扩展:
使用 psql 命令连接到 PostgreSQL 数据库服务器。
# su - postgres Last login: Sun Oct 10 14:04:03 EDT 2021 on pts/0 $ psql psql (13.4) Type "help" for help.
创建一个新的数据库。
postgres=# create database test1; CREATE DATABASE
连接到新创建的c 数据库。
postgres=# \c test1 You are now connected to database "test1" as user "postgres".
执行以下命令以启用 test1 数据库所需的 PostGIS 扩展。
test1=# CREATE EXTENSION postgis; CREATE EXTENSION test1=# CREATE EXTENSION postgis_raster; CREATE EXTENSION test1=# CREATE EXTENSION postgis_topology; CREATE EXTENSION test1=# CREATE EXTENSION postgis_sfcgal; CREATE EXTENSION test1=# CREATE EXTENSION fuzzystrmatch; CREATE EXTENSION test1=# CREATE EXTENSION address_standardizer; CREATE EXTENSION test1=# CREATE EXTENSION address_standardizer_data_us; CREATE EXTENSION test1=# CREATE EXTENSION postgis_tiger_geocoder; CREATE EXTENSION
现在,使用地理空间数据类型创建一个表和相应的索引。
test1=# CREATE TABLE mytable ( test1(# id SERIAL PRIMARY KEY, test1(# geom GEOMETRY(Point, 26910), test1(# name VARCHAR(128) test1(# ); CREATE TABLE test1=# CREATE INDEX mytable_gix test1-# ON mytable test1-# USING GIST (geom); CREATE INDEX
现在,在此表中插入一些测试数据。
test1=# INSERT INTO mytable (geom) VALUES ( test1(# ST_GeomFromText('POINT(0 0)', 26910) test1(# ); INSERT 0 1
现在,查询数据如下。
test1=# SELECT id, name test1-# FROM mytable test1-# WHERE ST_DWithin( test1(# geom, test1(# ST_GeomFromText('POINT(0 0)', 26910), test1(# 1000 test1(# ); id | name ----+----- 1 | (1 row)
从 psql 提示符退出。
test1=# \q $ exit logout
在 CentOS/RHEL 8 上安装 PostGIS 扩展:
PostGIS 所需的一些包在标准和 PostgreSQL yum 存储库中不可用,因此,我们需要安装 EPEL(企业 Linux 的另外包)yum 存储库。
# dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
我们还需要启用 PowerTools 存储库。
对于 CentOS/Rocky Linux,我们可以执行以下命令来启用 PowerTools 存储库。
# dnf -y config-manager --set-enabled PowerTools
而对于 RHEL,请执行以下命令。
# subscription-manager repos --enable codeready-builder-for-rhel-8-x86_64-rpms 1 local certificate has been deleted. Repository 'codeready-builder-for-rhel-8-x86_64-rpms' is enabled for this system.
为新安装的 yum 存储库构建缓存。
# dnf makecache
现在,我们可以在 PostgreSQL 服务器上安装 PostGIS 扩展。
PostgreSQL yum 存储库中有许多版本的 PostGIS 可用。
选择与数据库版本匹配的版本。
# dnf install -y postgis31_13
PostGIS 是 PostgreSQL 数据库的扩展,用于添加对地理空间数据的支持。