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 手冊
· PostgreSQL 學習手冊筆記
https://www.cnblogs.com/stephen-liu74/archive/2012/06/08/2315679.html
· PostgreSQL 常用操作總結
· PostGIS Reference
 
 
未完待續...
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM