PostgreSQL工具雜記

工作中會用到很多小工具杯缺,特此記錄。

批量導(dǎo)出表

只導(dǎo)出insert語句
pg_dump -h host -p 5432 -U postgres -a -t t1 -t t2 --inserts -f /opt/temp.sql -d mcsas
導(dǎo)出全部表結(jié)構(gòu)和模式
pg_dump -h host -p 5432 -U postgres -t t1 -t t2 -f /opt/temp.sql -d mcsas

數(shù)據(jù)庫備份

pg_dump -h master -p 5432-U postgres -w -f /home/postgres/test.backup test

數(shù)據(jù)庫還原

pg_restore -h master -p 5432-U postgres -w -d test /home/postgres/test.backup
如果是文本格式的dump裹纳,直接使用
psql的 \i xxx.backup

shp導(dǎo)入pg

使用pgadmin3的可視化工具,也可以通過命令行
用法:shp2pgsql [<options>] <shapefile> [[<schema>.]<table>]
OPTIONS:
-s : [<from>:]<srid> 設(shè)置 SRID字段,默認(rèn)0勉躺,不能和 -D一起使用侈百。
(-d|a|c|p) 常用的互斥操作選項
-d 刪除之前的表瓮下,重建一個表導(dǎo)入shp數(shù)據(jù)。
-a 將shp數(shù)據(jù)追加到已有的表钝域,在同一個schema下讽坏。
-c 創(chuàng)建一個新表,然后導(dǎo)入shp數(shù)據(jù)例证,不指定操作選項會默認(rèn)這個路呜。
-p 預(yù)備模式,只創(chuàng)建表织咧,不導(dǎo)入數(shù)據(jù)拣宰。
-g <geocolumn> 指定表的圖形列,(更多用在append模式下)
-D Use postgresql dump format (defaults to SQL insert statements).
-e 獨立執(zhí)行烦感,不使用事務(wù)巡社。.和-D不相容。
-G Use geography type (requires lon/lat data or -s to reproject).
-k Keep postgresql identifiers case.
-i 對dbf中所有的integer子彈使用int4類型
-I 對geocolumn創(chuàng)建空間索引手趣。
-m <filename> 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 創(chuàng)建單圖形而不是MULTI 圖形晌该。
-t <dimensionality> 強制指定圖形為 '2D', '3DZ', '3DM', or '4D'
-w Output WKT instead of WKB. Note that this can result in
coordinate drift.
-W <encoding> Specify the character encoding of Shape's
attribute column. (default: "UTF-8")
-N <policy> 空圖形策略 (insert*,skip,abort).
-n 只導(dǎo)入DBF文件
-T <tablespace> Specify the tablespace for the new table.
Note that indexes will still use the default tablespace unless the
-X flag is also used.
-X <tablespace> Specify the tablespace for the table's indexes.
This applies to the primary key, and the spatial index if
the -I flag is used.
-? Display this help screen.
舉例如下肥荔,導(dǎo)入一個shp,指定geomcolumn名稱為geom朝群,建立空間字段燕耿,圖形類型是單義圖形。
shp2pgsql -c -g geom -D -s 4326 -S -i -I shaperoads.shp myschema.roadstable | psql -d roadsdb

pg導(dǎo)出shp

用法: pgsql2shp [<options>] <database> [<schema>.]<table>
pgsql2shp [<options>] <database> <query>

OPTIONS:
-f <filename> 導(dǎo)出文件名稱
-h <host> 數(shù)據(jù)庫host
-p <port> 數(shù)據(jù)庫port
-P <password> 指定密碼
-u <user> 指定用戶
-g <geometry_column> 指定輸出geom列名稱
-b Use a binary cursor.
-r Raw mode. Do not assume table has been created by the loader. This would
not unescape attribute names and will not skip the 'gid' attribute.
-k Keep PostgreSQL identifiers case.
-m <filename> 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
-? Display this help screen.

舉例如下姜胖,將testdb數(shù)據(jù)中public的schema中test表導(dǎo)出為shp誉帅。
pgsql2shp -h host -p 5432 -u postgres -f /opt/test.shp testdb public.test

數(shù)據(jù)庫備庫

pg_basebackup -D $PGDATA -Fp -Xs -v -P -h master -p 5432 -U repuser

數(shù)據(jù)庫同步時間線

pg_rewind --target-pgdata=/home/postgres/data --source-server='host=slave port=5432 user=postgres dbname=postgres'

刪除表重復(fù)數(shù)據(jù)
DELETE FROM weather 
WHERE ctid 
NOT IN (
SELECT max(ctid) 
FROM weather 
GROUP BY city, temp_lo, temp_hi, prcp, date
);
跨表更新

update test t1 set field1=t2.field1 from test2 t2 where t1.id=t2.id

新建事務(wù)臨時表

create temp table tt(id int,name text) on commit drop; --事務(wù)結(jié)束就消失
create temp table tt(id int,name text) on commit delete rows; --事務(wù)結(jié)束數(shù)據(jù)消失
create temp table tt(id int,name text) on commit preserver rows; --數(shù)據(jù)存在整個會話周期中

賦予用戶讀取schema權(quán)限
alter default privileges in schema public grant all on tables to freerep;
alter default privileges in schema public revoke all on tables to freerep;
pgpool強制由master執(zhí)行sql
/*NO LOAD BALANCE*/ select * from abc;
強制斷開所有連接
select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname='tt';
copy導(dǎo)入csv
copy sexit from 'e:/sexit.csv' delimiter as '|' csv quote as '''';
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市右莱,隨后出現(xiàn)的幾起案子蚜锨,更是在濱河造成了極大的恐慌,老刑警劉巖慢蜓,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件亚再,死亡現(xiàn)場離奇詭異,居然都是意外死亡晨抡,警方通過查閱死者的電腦和手機氛悬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來耘柱,“玉大人如捅,你說我怎么就攤上這事〉骷澹” “怎么了伪朽?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長汛蝙。 經(jīng)常有香客問我烈涮,道長,這世上最難降的妖魔是什么窖剑? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任坚洽,我火速辦了婚禮,結(jié)果婚禮上西土,老公的妹妹穿的比我還像新娘讶舰。我一直安慰自己,他們只是感情好需了,可當(dāng)我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布跳昼。 她就那樣靜靜地躺著,像睡著了一般肋乍。 火紅的嫁衣襯著肌膚如雪鹅颊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天墓造,我揣著相機與錄音堪伍,去河邊找鬼锚烦。 笑死,一個胖子當(dāng)著我的面吹牛帝雇,可吹牛的內(nèi)容都是我干的涮俄。 我是一名探鬼主播,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼尸闸,長吁一口氣:“原來是場噩夢啊……” “哼彻亲!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起吮廉,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤苞尝,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后茧痕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡恼除,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年踪旷,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片豁辉。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡令野,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出徽级,到底是詐尸還是另有隱情气破,我是刑警寧澤,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布餐抢,位于F島的核電站现使,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏旷痕。R本人自食惡果不足惜碳锈,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望欺抗。 院中可真熱鬧售碳,春花似錦、人聲如沸绞呈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽佃声。三九已至艺智,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間圾亏,已是汗流浹背力惯。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工碗誉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人父晶。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓哮缺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親甲喝。 傳聞我的和親對象是個殘疾皇子尝苇,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容