Ubuntu18 + Postgres安装

 

Ubuntu18 + Postgres安装

参考:

 

http://trac.osgeo.org/postgis/wiki/UsersWikiPostGIS24UbuntuPGSQL10Apt

 

https://blog.csdn.net/sinat_23246437/article/details/54341755

 

https://winnie.postgis.net/download/windows/pg10/buildbot/archive/

 

http://www.postgis.net/install/

 

1、安装

 

安装完成后,默认会:

(1)创建名为"postgres"的Linux用户

(2)创建名为"postgres"、不带密码的默认数据库账号作为数据库管理员

(3)创建名为"postgres"的表

安装完成后的一些默认信息如下:

config /etc/postgresql/10/main

data /var/lib/postgresql/10/main

locale en_US.UTF-8

socket /var/run/postgresql

port 5432

 

2、psql命令

 

安装完后会有PostgreSQL的客户端psql,通过 sudo -u postgres psql 进入,提示符变成: postgres=#

 

在这里可用执行SQL语句和psql的基本命令。可用的基本命令如下:

 

\password:设置密码

\q:退出

\h:查看SQL命令的解释,比如\h select。

\?:查看psql命令列表。

\l:列出所有数据库。

\c [database_name]:连接其他数据库。

\d:列出当前数据库的所有表格。

\d [table_name]:列出某一张表格的结构。

\du:列出所有用户。

\e:打开文本编辑器。

\conninfo:列出当前数据库和连接的信息。

\connect:重连接数据库。

 

3、修改Linux用户的密码

 

opengis@gisserver20:~$ sudo passwd postgres

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

 

 

4、登录

 

使用psql命令登录数据库的命令为:

psql -U dbuser -d geodb -h 127.0.0.1 -p 5432

 

上面命令的参数含义如下:-U指定用户,-d指定数据库,-h指定服务器,-p指定端口。

 

输入上面命令以后,系统会提示输入dbuser用户的密码。

 

psql命令存在简写形式:如果当前Linux系统用户,同时也是PostgreSQL用户,则可以省略用户名(-U参数的部分);如果PostgreSQL内部还存在与当前系统用户同名的数据库,则数据库名也可以省略。

 

 

5、修改默认管理员账号的密码

 

opengis@gisserver20:~$ sudo su - postgres

postgres@gisserver20:~$ psql postgres

psql (10.6 (Ubuntu 10.6-0ubuntu0.18.04.1))

Type "help" for help.

 

postgres=# \password postgres

Enter new password:

Enter it again:

postgres=# alter user postgres with password 'gis'

 

postgres-# \q

postgres@gisserver20:~$ exit

logout

 

6、配置数据库以允许远程连接访问

 

6.1、修改监听地址

 

postgres@gisserver20:~$ vim /etc/postgresql/10/main/postgresql.conf

 

将 #listen_addresses = 'localhost' 的注释去掉并改为 listen_addresses = '*'

 

6.2、修改可访问用户的IP段

 

postgres@gisserver20:~$ vim /etc/postgresql/10/main/pg_hba.conf

 

在文件末尾添加: host all all 0.0.0.0 0.0.0.0 md5 ,表示运行任何IP连接

 

 

6.3、重启数据库

 

postgres@gisserver20:~$ sudo systemctl restart postgresql

 

7、添加新用户和新数据库

 

opengis@gisserver20:~$ sudo su - postgres

[sudo] password for opengis:

postgres@gisserver20:~$ psql postgres

psql (10.6 (Ubuntu 10.6-0ubuntu0.18.04.1))

Type "help" for help.

 

7.1、创建用户"zyx"并设置密码:

 

postgres=# create user zyx with password '123456';

 

7.2、创建数据库geodb,所有者为zyx

 

postgres=# create database geodb owner zyx;

 

7.3、将geodb数据库的所有权限赋予zyx

 

postgres=# grant all privileges on database geodb to zyx;

 

7.4、删除geodb数据库

 

postgres=# drop database geodb;

 

7.5、删除用户zyx

 

postgres=# drop user zyx;

 

 

8、基本数据库操作命令

 

# 创建新表

CREATE TABLE user_tbl(name VARCHAR(20), signup_date DATE);

 

# 插入数据

INSERT INTO user_tbl(name, signup_date) VALUES('张三', '2013-12-22');

 

# 选择记录

SELECT * FROM user_tbl;

 

# 更新数据

UPDATE user_tbl set name = '李四' WHERE name = '张三';

 

# 删除记录

DELETE FROM user_tbl WHERE name = '李四' ;

 

# 添加字段

ALTER TABLE user_tbl ADD email VARCHAR(40);

 

# 更新字段

ALTER TABLE user_tbl ALTER COLUMN signup_date SET NOT NULL;

 

# 更名字段

ALTER TABLE user_tbl RENAME COLUMN signup_date TO signup;

 

# 删除字段

ALTER TABLE user_tbl DROP COLUMN email;

 

# 表格更名

ALTER TABLE user_tbl RENAME TO backup_tbl;

 

# 删除表格

DROP TABLE IF EXISTS backup_tbl;

 

9、安装PostGIS

 

9.1、查看apt-get库中的软件版本支持

 

opengis@gisserver20:~$ sudo apt-cache search postgis

opengis@gisserver20:~$ sudo apt-cache search postgresql

 

9.2、安装PostGIS扩展

 

opengis@gisserver20:~$ sudo apt install postgresql-10-postgis-2.4

opengis@gisserver20:~$ sudo apt install postgresql-10-postgis-scripts

 

9.3、安装PostGIS命令行工具(shp2pgsql, raster2pgsql等)

 

opengis@gisserver20:~$ sudo apt-get install postgis

 

9.4、安装pgRouting

 

opengis@gisserver20:~$ sudo apt install postgresql-10-pgrouting

 

9.5、启用Adminpack

 

opengis@gisserver20:~$ sudo -u postgres psql

psql (10.6 (Ubuntu 10.6-0ubuntu0.18.04.1))

Type "help" for help.

 

postgres=# CREATE EXTENSION adminpack;

CREATE EXTENSION

postgres=#

 

10、创建空间数据库

 

10.1、创建用户

 

opengis@gisserver20:~$ sudo -u postgres psql

postgres=# create user gis with password 'gis';

postgres-# ALTER USER gis WITH SUPERUSER;

 

10.2、创建数据库

 

opengis@gisserver20:~$ sudo -u postgres psql

 

postgres=# CREATE DATABASE geodb WITH OWNER=gis ENCODING='UTF8';

CREATE DATABASE

postgres=#

postgres=# \connect geodb;

 

10.3、创建模式

 

postgres@gisserver20:~$ psql -U gis -d geodb -h 127.0.0.1 -p 5432;

 

geodb=> CREATE SCHEMA postgis;

CREATE SCHEMA

geodb=> ALTER DATABASE geodb SET search_path=public, postgis, contrib;

ALTER DATABASE

 

geodb=> \connect geodb;

 

10.4、添加扩展

 

geodb=> \connect geodb postgres;

geodb=# CREATE EXTENSION postgis SCHEMA postgis;

CREATE EXTENSION

geodb=# SELECT postgis_full_version();

------------------------------------------------

POSTGIS="2.4.3 r16312" PGSQL="100" GEOS="3.6.2-CAPI-1.10.2 4d2925d6" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.2.3, released 2017/11/20" LIBXML="2.9.4" LIBJSON="0.12.1" LIBPROTOBUF="1.2.1" RASTER

 

geodb=# CREATE EXTENSION pgrouting SCHEMA postgis;

CREATE EXTENSION

geodb=# SELECT * FROM pgr_version();

version | tag | hash | branch | boost

---------+--------+-----------+--------+--------

2.5.2 | v2.5.2 | 60585f1f7 | master | 1.65.1

(1 row)

 

geodb=# \q

opengis@gisserver20:~$ sudo systemctl restart postgresql

 

11、导入SHP数据到空间数据库

 

https://postgis.net/docs/using_postgis_dbmanagement.html#shp2pgsql_usage

 

 

 

import subprocess

p = subprocess.Popen(["shp2pgsql", "layer.shp", "schemaName.tableName"], stdout=subprocess.PIPE)

output, err = p.communicate()

 

 

 shp2pgsql: Using the ESRI Shapefile Loader

The shp2pgsql data loader converts ESRI Shape files into SQL suitable for insertion into a PostGIS/PostgreSQL database either in geometry or geography format. The loader has several operating modes distinguished by command line flags:

In addition to the shp2pgsql command-line loader, there is an shp2pgsql-gui graphical interface with most of the options as the command-line loader, but may be easier to use for one-off non-scripted loading or if you are new to PostGIS. It can also be configured as a plugin to PgAdminIII.

(c|a|d|p) These are mutually exclusive options:

-c

Creates a new table and populates it from the shapefile. This is the default mode.

-a

Appends data from the Shape file into the database table. Note that to use this option to load multiple files, the files must have the same attributes and same data types.

-d

Drops the database table before creating a new table with the data in the Shape file.

-p

Only produces the table creation SQL code, without adding any actual data. This can be used if you need to completely separate the table creation and data loading steps.

-?

Display help screen.

-D

Use the PostgreSQL "dump" format for the output data. This can be combined with -a, -c and -d. It is much faster to load than the default "insert" SQL format. Use this for very large data sets.

-s [<FROM_SRID%gt;:]<SRID>

Creates and populates the geometry tables with the specified SRID. Optionally specifies that the input shapefile uses the given FROM_SRID, in which case the geometries will be reprojected to the target SRID. FROM_SRID cannot be specified with -D.

-k

Keep identifiers' case (column, schema and attributes). Note that attributes in Shapefile are all UPPERCASE.

-i

Coerce all integers to standard 32-bit integers, do not create 64-bit bigints, even if the DBF header signature appears to warrant it.

-I

Create a GiST index on the geometry column.

-m

-m a_file_name Specify a file containing a set of mappings of (long) column names to 10 character DBF column names. The content of the file is one or more lines of two names separated by white space and no trailing or leading space. For example:

COLUMNNAME DBFFIELD1

AVERYLONGCOLUMNNAME DBFFIELD2

-S

Generate simple geometries instead of MULTI geometries. Will only succeed if all the geometries are actually single (I.E. a MULTIPOLYGON with a single shell, or or a MULTIPOINT with a single vertex).

-t <dimensionality>

Force the output geometry to have the specified dimensionality. Use the following strings to indicate the dimensionality: 2D, 3DZ, 3DM, 4D.

If the input has fewer dimensions that specified, the output will have those dimensions filled in with zeroes. If the input has more dimensions that specified, the unwanted dimensions will be stripped.

-w

Output WKT format, instead of WKB. Note that this can introduce coordinate drifts due to loss of precision.

-e

Execute each statement on its own, without using a transaction. This allows loading of the majority of good data when there are some bad geometries that generate errors. Note that this cannot be used with the -D flag as the "dump" format always uses a transaction.

-W <encoding>

Specify encoding of the input data (dbf file). When used, all attributes of the dbf are converted from the specified encoding to UTF8. The resulting SQL output will contain a SET CLIENT_ENCODING to UTF8command, so that the backend will be able to reconvert from UTF8 to whatever encoding the database is configured to use internally.

-N <policy>

NULL geometries handling policy (insert*,skip,abort)

-n

-n Only import DBF file. If your data has no corresponding shapefile, it will automatically switch to this mode and load just the dbf. So setting this flag is only needed if you have a full shapefile set, and you only want the attribute data and no geometry.

-G

Use geography type instead of geometry (requires lon/lat data) in WGS84 long lat (SRID=4326)

-T <tablespace>

Specify the tablespace for the new table. Indexes will still use the default tablespace unless the -X parameter is also used. The PostgreSQL documentation has a good description on when to use custom tablespaces.

-X <tablespace>

Specify the tablespace for the new table's indexes. This applies to the primary key index, and the GIST spatial index if -I is also used.

An example session using the loader to create an input file and uploading it might look like this:

# shp2pgsql -c -D -s 4269 -i -I shaperoads.shp myschema.roadstable > roads.sql

# psql -d roadsdb -f roads.sql

A conversion and upload can be done all in one step using UNIX pipes:

# shp2pgsql shaperoads.shp myschema.roadstable | psql -d roadsdb

 

 

11、导出空间数据库到SHP数据

 

Using the Dumper

The pgsql2shp table dumper connects directly to the database and converts a table (possibly defined by a query) into a shape file. The basic syntax is:

pgsql2shp [<options>] <database> [<schema>.]<table>

pgsql2shp [<options>] <database> <query>

The commandline options are:

-f <filename>

Write the output to a particular filename.

-h <host>

The database host to connect to.

-p <port>

The port to connect to on the database host.

-P <password>

The password to use when connecting to the database.

-u <user>

The username to use when connecting to the database.

-g <geometry column>

In the case of tables with multiple geometry columns, the geometry column to use when writing the shape file.

-b

Use a binary cursor. This will make the operation faster, but will not work if any NON-geometry attribute in the table lacks a cast to text.

-r

Raw mode. Do not drop the gid field, or escape column names.

-m filename

Remap identifiers to ten character names. The content of the file is lines of two symbols separated by a single white space and no trailing or leading space: VERYLONGSYMBOL SHORTONE ANOTHERVERYLONGSYMBOL SHORTER etc.

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/gispathfinder/p/10476683.html
今日推荐