PostgreSQL | 学习笔记&语句汇总

PostgreSQL 是世界上功能最强大的开源关系型数据库,除了标准的 SQL 的支持外,还有丰富的特性,可以通过扩展来增强数据库的能力,如uuid-ossp,pgcrypto,hstore,PostGIS等扩展。支持如 hstore、range、GIS、json 等更多的数据类型。此外,Greenplum也是基于PG来提供结构化大数据分析仓库解决方案。本文整理了postgre常用基本操作,包括:命令行操作、查询语句以及postgis空间操作等相关语句。

1 命令行操作

postgresql备份:

pg_dump -U postgres -d gisportal -F c -f i:\db2.dump

postgresql还原:

pg_restore -U postgres -d gisportal i:\db2.dump
pg_restore -U postgres -d gisportal e:\postgres-20150420.backup

备份单张表:

pg_dump -U postgres -F c -b -v -f i:\test.bak -t sqs.test gisportal

备份整个数据库(非模式):

pg_dump -U postgres -F c -b -v -f i:\test.bak -t sqs.test gisportal 

备份数据库下的模式:         

pg_dump -U postgres -F c -b -v -f i:\test.bak -n sqs gisportal

还原单张表:

pg_restore -U postgres -W -d gisportal -v "d:\poi.bak"

要执行语句,先执行:cd postgresql安装目录bin中

pg_dump -U postgres  -F c -f i:\gisportal.dump gisportal(备份整个数据库,备份单个模式表为另一种写法)
pg_dump -h localhost -U postgres(用户名) 数据库名(缺省时同用户名)  -t table(表名) >/data/dum.sql
pg_dump -h localhost -U postgres gisportal  -t 省界_region >i:\pros.sql

2 基本操作

创建表格

create table schema_name."table_name"(
id integer,
name character varying,
geom geometry)

新建表格并复制某表内容

create table table1 
as 
select * from table2

插入另一张表的整列:

insert into sqs.sqs_role(username) select username from sqs.sqs_user

添加多个列

alter table "table_name" 
add "num_1" INTEGER, 
add "num_2" INTEGER,
add "length" FLOAT
...

更新列

UPDATE table1
   set pop1 = a.pop
  from 
(select "OID", pop
   from "table2"
  where  pop != 0) a
 where table1."OID" = a."OID"

列类型转换

1)表格新建一列
update table1
set num1 = num1::integer

2)直接修改
alter table table_1
alter column num1 type integer

查询表的字段类型

select column_name, data_type 
from information_schema.columns 
where table_name = 'gridbuffer_wgs'

设置null为零值

update schema."table_name" 
   set "column_name" = 0
 where "column_name" is null;

插入当天日期date:

update sqs.sqs_shopmall set version=current_date;

截取字符:

update sqs.sqs_poi2 set prcode=substring(prcode,1,2)  (index从1开始,1保留)

空字符串处理:

update property set memorial_no = btrim(memorial_no, ' ') where memorial_no like ' %'
update property set memorial_no = 2  where memorial_no= btrim(memorial_no, ' ')

多条件模糊查询:

SELECT * FROM table WHERE column LIKE 'Text%' OR column LIKE 'Link%' OR column LIKE 'Hello%' OR column LIKE '%World%'

只查询前几条数据:

LIMIT num  OFFSET startNum //可以实现指定条数开始取几条

 替换replace:

update cz_basedata_test2 set cnty=replace(cnty,city,'')//不可为null,否则整列将会变为null

3 数值统计

类别统计 (case when语句)

select b.id, sum(case when c.category = '公交车站' then 1 end) "公交车站" 
  from schema."table_name" b left join schema.table_2 c
                                    on st_intersects(b.geom, c.geom)
and c.cityname = '上海'
group by b.id

4 PostGIS空间操作

查询投影类型

select st_srid(geom) from table

投影类型转换

update table_1
   set geom_4326 = st_transform(geom, 4326)

计算路网长度

select b.id, sum(st_length(st_intersection(geography(b.geom),a.geom))) length
  from schema."table1" b  left join schema2.table2 a
                                 on st_intersects(a.geom, b.geom)
and a.city = '深圳'
group by b.id

计算相交面积

ST_Area(ST_INTERSECTION(b.geom_gcj, c.geom))

计算缓冲区

st_buffer(geography(a.geom_gcj), 3000)
-- 计算完后需要再进行geometry转换
geometry(st_buffer(geography(a.geom_gcj), 5000))

计算面积(根据geometry计算面积)

update sqs.sqs_town 
   set totarea=st_area(geography(geom))/1000000

计算距离(包含经纬度转换地理坐标)

select name,st_astext(geom) as geomtext, st_distance(Geography(ST_PointFromText('POINT(121.43543 31.2399)',4326)),Geography(geom)) as distance from table_1
 where ST_Intersects(st_buffer(geography(ST_PointFromText('POINT(121.43543 31.2399)',4326)),3000),geom) 
   and dlvydate> date '2010-01-01'

交叉计算: select st_astext(geom) as GeomText ,”County” ,jdlon as Centerx , jdlat as Centery ,( people::DECIMAL/area::DECIMAL*1000000.0 )::INT as Density from vtown where ST_Intersects(st_buffer( geography( ST_PointFromText(‘POINT(121.492498 31.229649)’,4326)),4000),geom)   数组转换为geometry(对百度数组也适用):

select  name from sqs.sqs_pipeline where ST_Intersects(ST_GeomFromText('POLYGON((121.550391 31.048365,121.845897 31.063214,121.734364 30.859081,121.580286 30.940391,121.550391 31.048365))',4326),geom)
select  name from sqs.sqs_pipeline where ST_Within(geom,ST_PolygonFromText('POLYGON((121.550391 31.048365,121.845897 31.063214,121.734364 30.859081,121.580286 30.940391,121.550391 31.048365))',4326)) and status=1 

根据geom计算x、y:

update sqs.sqs_poi set lng=st_x(geom),lat=st_y(geom) 

获取点geometry的点x、y坐标:

select *, st_x(geom), st_y(geom) 
from sqs.sqs_store
where status=1
and username='admin'
and ST_Within(geom,ST_GeomFromText('POLYGON((121.289092 31.38394,121.333648 31.275857,121.462429 31.333862,121.459267 31.38172,121.379928 31.405393,121.289092 31.38394))',4326)) 

点转换为geometry:

update sqs.sqs_pipeline 
set pipelineid='pp00000008', name='admin', geom=ST_PointFromText('POINT(121.845897 31.063214 )',4326),status=1;
update sqs.yichang
set geom=ST_PointFromText('POINT('||lng||' '||lat||')',4326); update sqs.sqs_pipeline_wfztest set geomta500=ST_PolygonFromText(st_astext(st_buffer(geography(ST_PointFromText('POINT('||st_x(geom)||' '||st_y(geom)||')',4326)), 500 )) ,4326); String sql = "select st_Area(Geography(st_intersection(a.geom,b.geom)))/st_Area(geography(a.geom)) as per100 " + "from sqs.sqs_city a, "+tablename +" b " + "where st_Intersects(a.geom,b.geom) and b."+id+"='"+ storeid+ "' and b.username='"+username +"' and b.status=1" ; String sql = "select ceil(totarea) as totarea,st_asgeojson(b.geom) as geometry, ceil(st_distance(Geography(ST_PointFromText('POINT(" + x + " " + y +")',4326)),Geography(b.geom))) as distance from sqs.sqs_newhouse b " + "where ST_Intersects(ST_PolygonFromText('POLYGON(("+p+"))',4326), b.geom) and " +"dlvydate > '2010-1-1' " + "order by tothh desc " + "limit 10";

 

学习资源 · PostgreSQL 10.1 手册
http://www.postgres.cn/docs/10/index.html · PostgreSQL 学习手册笔记 https://www.cnblogs.com/stephen-liu74/archive/2012/06/08/2315679.html · PostgreSQL 常用操作总结
https://www.cnblogs.com/kaituorensheng/p/4667160.html#_label3 · PostGIS Reference
https://postgis.net/docs/reference.html     未完待续…  

    原文作者:PostgreSQL
    原文地址: https://www.cnblogs.com/geo-will/p/9535421.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞