1. 程式人生 > >PostgreSQL | 常用語句記錄

PostgreSQL | 常用語句記錄

alt 模糊查詢 te pro 目錄 用戶名 lec 不可 HERE 時間

一、基礎操作

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 插入另一張表的整列:insert into sqs.sqs_role(username) select username from sqs.sqs_user 插入當天日期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%‘

二、空間操作

計算面積(根據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 cz_basedata_other_l 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"; 建立一張新表並從另一張已存在的表復制表結構及數據: create table town_20150525 as select * from vtown_20150526 添加字段(自增長序列): alter table cz_users add column gender character varying(50) unique alter table cz_udatarange add column id serial unique 唯一值 timestamp without time zone DEFAULT now(),時間 yyyy-MM-dd hh:mm:ss 單獨添加列 空間面:select addgeometrycolumn(‘sqs‘,‘sqs_province‘,‘geom‘,4326,‘MULTIPOLYGON‘,2,TRUE) 前幾條數據:LIMIT num OFFSET startNum 可以實現指定條數開始取幾條 替換replace:update cz_basedata_test2 set cnty=replace(cnty,city,‘‘)//不可為null,否則整列將會變為null sqs.sqs_pineline 等表中的pipelineid、userid賦值方法: update sqs.sqs_pipeline set userid=‘24c19d21-f519-4a49-869d-6455787537a3‘,status=1,pipelineid=‘pp‘||to_char(id, ‘FM00000000‘),name=‘pp‘||to_char(id, ‘FM00000000‘) where userid is null update sqs.sqs_competitor set storeid=‘cc‘||to_char(id,‘FM00000000‘),userid=‘e27accfb-e5ff-409c-830f-4a67387237ff‘ where userid is null update sqs.sqs_store set userid=‘55bf68a3-adf2-49d6-89f8-aa05f59cbe01‘,username=‘xxx‘,status=1,storeid=to_char(id, ‘FM00000000‘) where userid is null sql語句中+號:update sqs.sqs_streets set geom=ST_PointFromText(‘POINT(‘||lng||‘ ‘||lat||‘)‘,4326)

PostgreSQL | 常用語句記錄