一、在线安装pgsql12
1、进入官网
postgresql官网:https://www.postgresql.org/
postgresql中文社区:http://www.postgres.cn/v2/document
2、选择系统版本
选择之后等待网页自动跳转
3、选择合适版本
网页下拉找到 PostgreSQL Yum Repository ,选择安装的版本,之后复制安装命令并执行
代码语言:javascript复制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 ,执行启动服务命令
代码语言:javascript复制 postgresql-setup --initdb
systemctl enable postgresql.service
systemctl start postgresql.service
6、查看pgsql服务
代码语言:javascript复制ps -ef | grep postgres
二、配置pgsql12
1、修改账户密码
切换系统角色
代码语言:javascript复制su postgres
命令行登录pgsql
代码语言:javascript复制psql
重新设置密码
代码语言:javascript复制password postgres
查看密码(已经过MD5加密)
代码语言:javascript复制SELECT rolname,rolpassword FROM pg_authid;
在线安装默认路径
代码语言:javascript复制/var/lib/pgsql/12/data/
/usr/bin/postgresql-12-setup
/usr/pgsql-12/bin/postgresql-12-setup
2、运行远程连接
修改pg_hba.conf配置文件,添加允许其他机器访问
代码语言:javascript复制su postgres
vim /var/lib/pgsql/12/data/pg_hba.conf
添加以下代码
代码语言:javascript复制host all all 0.0.0.0/0 password
修改 postgresql.conf,将监听修改为监听所有主机的请求
代码语言:javascript复制vim /var/lib/pgsql/12/data/postgresql.conf
搜索配置位置 :/listen,添加以下代码
代码语言:javascript复制listen_addresses = '*'
3、重启postgresql
代码语言:javascript复制systemctl restart postgresql-12
systemctl status postgresql-12
三、添加postgis依赖
postgis官网:http://www.postgis.org/
postgis官方文档:http://postgis.net/documentation/
postgis用户手册:http://download.osgeo.org/postgis/docs/postgis-3.1.1.pdf
postgis源码编译:http://postgis.net/docs/postgis_installation.html#install_short_version
1、安装postgis依赖
为CentOS7/RHEL7安装EPEL 仓库(repo)
代码语言:javascript复制yum -y install epel-release
yum在线安装postgis
代码语言:javascript复制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依赖只对当前数据库有效
代码语言:javascript复制-- 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;
3、使用空间函数
代码语言:javascript复制-- 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查看创建的空间函数表
4、查看安装版本
代码语言:javascript复制SELECT version();
SELECT PostGIS_full_version();