Centos7安装PostgreSQL 12.4 + postgis安装

Centos7安装PostgreSQL 12.4 + postgis安装

环境:Centos7.8-Mini + PostgreSQL 12.4 + 

# 关闭防火墙
systemctl  stop firewalld
systemctl  disable firewalld

vim /etc/selinux/config
SELINUX=disabled

# 立即生效
setenforce 0

# 安装PostgreSQL yum仓库
yum install -y https://download.postgresql.org/pub/repos/yum/12/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

也可以通过下面路径获取最新PostgreSQL yum仓库
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm


#查看yum源的所有PostgreSQL所有版本
cat /etc/yum.repos.d/pgdg-redhat-all.repo |grep PostgreSQL
yum search postgresql

yum search postgresql |grep postgresql12                  
postgresql12.x86_64 : PostgreSQL client programs and libraries
postgresql12-contrib.x86_64 : Contributed source and binaries distributed with
postgresql12-devel.x86_64 : PostgreSQL development header files and libraries
postgresql12-docs.x86_64 : Extra documentation for PostgreSQL
postgresql12-libs.x86_64 : The shared libraries required for any PostgreSQL
postgresql12-llvmjit.x86_64 : Just-in-time compilation support for PostgreSQL
postgresql12-odbc.x86_64 : PostgreSQL ODBC driver
postgresql12-odbc-debuginfo.x86_64 : Debug information for package
                                   : postgresql12-odbc
postgresql12-plperl.x86_64 : The Perl procedural language for PostgreSQL
postgresql12-plpython.x86_64 : The Python procedural language for PostgreSQL
postgresql12-plpython3.x86_64 : The Python3 procedural language for PostgreSQL
postgresql12-pltcl.x86_64 : The Tcl procedural language for PostgreSQL
postgresql12-server.x86_64 : The programs needed to create and run a PostgreSQL
postgresql12-tcl.x86_64 : A Tcl client library for PostgreSQL
postgresql12-test.x86_64 : The test suite distributed with PostgreSQL

# 通过yum安装 PostgreSQL Client packages & PostgreSQL Server packages
yum -y install postgresql12
yum install -y postgresql12-server
默认安装路径为:/usr/pgsql-12 目录

# contrib 是一些第三方组织贡献出来的一些工具,在日常维护中也很有用,如果需要的话,也可以安装上
yum install -y postgresql12-contrib

# 初始化数据库
/usr/pgsql-12/bin/postgresql-12-setup initdb

# 启动服务&设置开机自启动
systemctl start postgresql-12
systemctl enable postgresql-12


# 修改数据库配置
vim /var/lib/pgsql/12/data/postgresql.conf

listen_addresses = '*'
port = 5432


vim /var/lib/pgsql/12/data/pg_hba.conf
末尾添加下面类容,不限制任何主机并允许远程登录:
host    all             all             0.0.0.0/0               md5

# 修改后重启数据库
systemctl  restart postgresql-12

# 查看服务监听状态
netstat -an |grep 5432


PostgreSQL 安装后会默认创建一个 postgres 用户
# 切换到 postgres 用户
su - postgres

# 登录数据库shell
psql -U postgres
postgres=# ALTER USER postgres with encrypted password '1q2w3e';
ALTER ROLE
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)


\q
exit


查看当前连接用户
select * from current_user;
select user;
\du;

执行结果如下:
postgres=# select * from current_user;
 current_user 
--------------
 postgres
(1 row)

postgres=# select user;
   user   
----------
 postgres
(1 row)

postgres=# \du;
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# 


常用操作命令(PostgreSQL常用命令可参考:https://blog.csdn.net/sunny05296/article/details/108466453)

创建数据库新用户:
postgres=# CREATE USER test WITH PASSWORD '1q2w3e';
注意:语句要以分号结尾,密码要用单引号括起来。

创建用户数据库
postgres=# CREATE DATABASE testdb01 OWNER test;

将数据库的所有权限赋予用户
postgres=# GRANT ALL PRIVILEGES ON DATABASE testdb01 TO test;

获取当前db中所有的表信息:
select * from pg_tables;

查看用户的所有表(用户自定义的表,如果未经特殊处理,默认都是放在名为public的schema下)
postgres=# select tablename from pg_tables where schemaname='public';

创建表:
postgres=# create table test01( 
id integer not null, name character(255) not null,
price decimal(8,2) not null,
primary key(id)
);

插入数据
postgres=# insert into test01(id,name,price) values (1,'a',11.5),(2,'b',20.3);

查看表结构
\d test01;

查看表的数据
select * from test01;

退出重启数据库后,以用户test登录报错:

-bash-4.2$ psql -U test testdb01
psql: error: could not connect to server: FATAL:  Peer authentication failed for user "test"

以postgres用户可以正常登录:
-bash-4.2$ psql -U postgres testdb01
psql (12.4)
Type "help" for help.
testdb01=# 

报错原因:
psql的连接建立于Unix Socket上默认使用peer authentication,所以必须要用和数据库用户相同的系统用户进行登录。
还有一种方法,将peer authentiction 改为 md5,并给数据库设置密码。修改配置文件/var/lib/pgsql/12/data/pg_hba.conf,将
local   all             all                                     peer
local   replication     all                                     peer
两行配置的peer改成md5,修改后的内容如下:

vim /var/lib/pgsql/12/data/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
#local   all             all                                     peer
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     all                                     peer
local   replication     all                                     md5
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident
host    all             all             0.0.0.0/0               md5

重启数据库
systemctl  restart postgresql-12
su - postgres
-bash-4.2$ psql -U test testdb01
Password for user test: 
psql (12.4)
Type "help" for help.

testdb01=> 
登录成功

安装postgis

先介绍一下ArcGIS和PostGIS概念:
安装PostgreSQL数据库,创建用户sde,数据库test,架构名sde。ArcGIS就能连接了,但如果要使用,还要安装PostGIS插件,然后执行create extension postgis;这样该库就有了一个空间类型postgis,就可以成功创建要素类了。
有了空间类型postgis以后,再执行create enterprise geodatabase工具来创建Geodatabase模型。完成Geodatabase模型创建后,该库就有了两种空间类型,一种是ArcGIS的st_geometry,一种是PostGIS的geometry。而ArcGIS是可以兼容PostGIS类型的,所以创建要素类时,可以选择使用哪种类型,default就是ArcGIS的st_geometry,而pg_geometry是PostGIS的geometry。


安装postgis
1.安装工具包
yum install -y wget net-tools epel-release

2.安装postgis
yum install -y postgis30_12 postgis30_12-client

3.安装拓展工具
yum install -y ogr_fdw12
yum install -y pgrouting_12

注意:安装是注意版本要和PostgreSQL的版本一致,postgis和拓展工具的版本也要保持一致

4.创建数据库spatial_testdb
# create database spatial_testdb OWNER postgres;
也可以不单独创建,直接使用已有的数据库 testdb01

5.进入指定的数据库安装postgis扩展插件(开启postgis插件)
注意:需要使用 postgres 用户
psql -U postgres testdb01
create extension postgis;
create extension postgis_topology;
下面如果没有需求,也可以先不开启
##create extension ogr_fdw;
##create extension fuzzystrmatch;
##create extension address_standardizer;
##create extension address_standardizer_data_us;
##create extension postgis_tiger_geocoder;

6.验证是否安装成功
SELECT postgis_full_version();

testdb01=# SELECT postgis_full_version();
                                                                           postgis_full_version                                                                            
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 POSTGIS="3.0.2 2fb2a18" [EXTENSION] PGSQL="120" GEOS="3.8.1-CAPI-1.13.3" PROJ="7.1.1" LIBXML="2.9.1" LIBJSON="0.11" LIBPROTOBUF="1.0.2" WAGYU="0.4.3 (Internal)" TOPOLOGY
(1 row)

testdb01=# 


SELECT ST_SetSRID(ST_Point(-87.71, 43.741), 4326), ST_GeomFromText('POINT(-87.71 43.741)', 4326)


8.创建空间数据表
CREATE TABLE geom_test01(id integer not null, name varchar(255), primary key(id));
SELECT AddGeometryColumn('geom_test01', 'zone_geom', 4326, 'POINT', 2);
INSERT INTO geom_test01(id, zone_geom, name) VALUES (1, ST_GeomFromText('POINT(-0.1250 52.500)',4326), 'test');
INSERT INTO geom_test01(id, zone_geom, name) VALUES (2, ST_GeomFromText('POINT(27.91162480 -33.01532)', 4326),'test');

SELECT * FROM geom_test01;
SELECT id, ST_AsText(zone_geom), ST_AsEwkt(zone_geom), ST_X(zone_geom), ST_Y(zone_geom) FROM geom_test01;


testdb01=# SELECT * FROM geom_test01;
 id | name |                     zone_geom                      
----+------+----------------------------------------------------
  1 | test | 0101000020E6100000000000000000C0BF0000000000404A40
  2 | test | 0101000020E6100000F8382E3E60E93B40C47C7901F68140C0
(2 rows)

testdb01=# SELECT id, ST_AsText(zone_geom), ST_AsEwkt(zone_geom), ST_X(zone_geom), ST_Y(zone_geom) FROM geom_test01;
 id |          st_astext          |               st_asewkt               |    st_x    |   st_y    
----+-----------------------------+---------------------------------------+------------+-----------
  1 | POINT(-0.125 52.5)          | SRID=4326;POINT(-0.125 52.5)          |     -0.125 |      52.5
  2 | POINT(27.9116248 -33.01532) | SRID=4326;POINT(27.9116248 -33.01532) | 27.9116248 | -33.01532
(2 rows)

testdb01=# 

CREATE TABLE geom_test02(
id integer not null,
zone_geom geometry(point, 4326),
name varchar(255),
primary key(id)
);

INSERT INTO geom_test02(id, zone_geom, name) values (1, st_geomfromtext('point(27.91162480 -33.01532)', 4326), 'aaa');
SELECT * FROM geom_test02;
SELECT id, ST_AsText(zone_geom), ST_AsEwkt(zone_geom), ST_X(zone_geom), ST_Y(zone_geom) FROM geom_test02;

testdb01=# SELECT * FROM geom_test02;
 id |                     zone_geom                      | name 
----+----------------------------------------------------+------
  1 | 0101000020E6100000F8382E3E60E93B40C47C7901F68140C0 | aaa
(1 row)

testdb01=# SELECT id, ST_AsText(zone_geom), ST_AsEwkt(zone_geom), ST_X(zone_geom), ST_Y(zone_geom) FROM geom_test02;
 id |          st_astext          |               st_asewkt               |    st_x    |   st_y    
----+-----------------------------+---------------------------------------+------------+-----------
  1 | POINT(27.9116248 -33.01532) | SRID=4326;POINT(27.9116248 -33.01532) | 27.9116248 | -33.01532
(1 row)

你可能感兴趣的:(数据库,工具)