目录

​一、在线安装pgsql12​

​1、进入官网​

​2、选择系统版本​

​3、选择合适版本​

​4、安装启动服务​

​6、查看pgsql服务​

​二、配置pgsql12​

​1、修改账户密码​

​2、运行远程连接​

​3、重启postgresql​

​三、添加postgis依赖​

​1、安装postgis依赖​

​2、pgsql添加postgis拓展​

​3、使用空间函数​

​4、查看安装版本​


一、在线安装pgsql12

1、进入官网

postgresql官网:​​PostgreSQL: The world's most advanced open source database​

postgresql中文社区:​​文档目录/Document Index: 世界上功能最强大的开源数据库...​

Linux在线安装部署postgresql12+postgis3.1_sql

2、选择系统版本

选择之后等待网页自动跳转

Linux在线安装部署postgresql12+postgis3.1_sql_02

3、选择合适版本

网页下拉找到 PostgreSQL Yum Repository ,选择安装的版本,之后复制安装命令并执行

Linux在线安装部署postgresql12+postgis3.1_官网_03

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql12-server
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl enable postgresql-12
sudo systemctl start postgresql-12

4、安装启动服务

继续往下拉,找到 Post-installation ,执行启动服务命令

Linux在线安装部署postgresql12+postgis3.1_postgresql_04

postgresql-setup --initdb
systemctl enable postgresql.service
systemctl start postgresql.service

6、查看pgsql服务

ps -ef | grep postgres

Linux在线安装部署postgresql12+postgis3.1_sql_05

二、配置pgsql12

1、修改账户密码

切换系统角色

su postgres

命令行登录pgsql

psql

重新设置密码

\password postgres

查看密码(已经过MD5加密)

SELECT rolname,rolpassword FROM pg_authid;

Linux在线安装部署postgresql12+postgis3.1_postgresql_06

在线安装默认路径

/var/lib/pgsql/12/data/
/usr/bin/postgresql-12-setup
/usr/pgsql-12/bin/postgresql-12-setup

2、运行远程连接

修改pg_hba.conf配置文件,添加允许其他机器访问

su postgres
cp /var/lib/pgsql/12/data/pg_hba.conf /var/lib/pgsql/12/data/pg_hba.conf.initial
vim /var/lib/pgsql/12/data/pg_hba.conf

添加以下代码

host    all             all             0.0.0.0/0               password

Linux在线安装部署postgresql12+postgis3.1_官网_07

修改 postgresql.conf,将监听修改为监听所有主机的请求

cp /var/lib/pgsql/12/data/postgresql.conf /var/lib/pgsql/12/data/postgresql.conf.initial
vim /var/lib/pgsql/12/data/postgresql.conf

搜索配置位置 :/listen,添加以下代码

listen_addresses = '*'

3、重启postgresql

systemctl restart postgresql-12
systemctl status postgresql-12

三、添加postgis依赖

postgis官网:​​PostGIS : Home​

postgis官方文档:​​PostGIS — Documentation​

postgis用户手册:​​http://download.osgeo.org/postgis/docs/postgis-3.1.1.pdf​

postgis源码编译:​​Chapter 2. PostGIS Installation​

1、安装postgis依赖

为CentOS7/RHEL7安装EPEL 仓库(repo)

yum -y install epel-release

yum在线安装postgis

yum -y install postgis31_12.x86_64 postgis31_12-client.x86_64 postgis31_12-devel.x86_64 postgis31_12-docs.x86_64 postgis31_12-gui.x86_64 postgis31_12-utils.x86_64

2、pgsql添加postgis拓展

注意:扩展的postgis依赖只对当前数据库有效

-- create database
CREATE DATABASE testdb;
-- select database
\c testdb;
-- Enable PostGIS (as of 3.0 contains just geometry/geography)
CREATE EXTENSION postgis;
-- enable raster support (for 3+)
CREATE EXTENSION postgis_raster;
-- 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;

Linux在线安装部署postgresql12+postgis3.1_postgresql_08

3、使用空间函数

-- Create table with spatial column
CREATE TABLE mytable (
id SERIAL PRIMARY KEY,
geom GEOMETRY(Point, 26910),
name VARCHAR(128)
);

-- Add a spatial index
CREATE INDEX mytable_gix
ON mytable
USING GIST (geom);

-- Add a point
INSERT INTO mytable (geom) VALUES (
ST_GeomFromText('POINT(0 0)', 26910)
);

-- Query for nearby points
SELECT id, name
FROM mytable
WHERE ST_DWithin(
geom,
ST_GeomFromText('POINT(0 0)', 26910),
1000
);

Navicat连接pgsql查看创建的空间函数表

Linux在线安装部署postgresql12+postgis3.1_官网_09Linux在线安装部署postgresql12+postgis3.1_postgresql_10

4、查看安装版本

SELECT version();
SELECT PostGIS_full_version();

Linux在线安装部署postgresql12+postgis3.1_官网_11