




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
自動(dòng)化測(cè)試環(huán)境SigmationTF中PostgreSQL的使用指導(dǎo)訪問(wèn)postgres數(shù)據(jù)庫(kù)為保證可以訪問(wèn)postgres數(shù)據(jù)庫(kù),必需要設(shè)置root的權(quán)限(Allowroottorunanycommandsanywhere)[root@MC/var/log]#cd/etc[root@MC/etc]#chmod666sudoers[root@MC/etc]#vimsudoerssupportALL=(ALL)NOPASSWD:ALLadminALL=(ALL)NOPASSWD:/sbin/ifconfig,/sbin/reboot,/sbin/poweroff,/usr/sbin/network_config_save,\/usr/sbin/update_ip,/sbin/ethtool,/bin/hostname,/sbin/route,/opt/sigma-rt/tf/tools/reloadtfadminALL=(ALL)/usr/sbin/update_image,/usr/sbin/install_lic,/usr/sbin/vmware-install.sh,/usr/sbin/vmware-uninstall.shapacheALL=(ALL)NOPASSWD:/usr/bin/crontab,/usr/sbin/ldap_config##必需添加如下配置,否則,無(wú)法訪問(wèn)rootALL=(ALL)ALL使用命令sudo-upostgrespsql訪問(wèn)postgres數(shù)據(jù)庫(kù)。sudo-upostgrespsqlpostgres=#\lListofdatabasesName|Owner|Encoding-----------+----------+----------postgres|postgres|UTF8sat|postgres|UTF8template0|postgres|UTF8template1|postgres|UTF8(4rows)連接sigmationTF的后臺(tái)數(shù)據(jù)庫(kù)sat。postgres=#\csat;Youarenowconnectedtodatabase"sat".列出sat的所有的表。sat=#\dtListofrelationsSchema|Name|Type|Owner--------+-------------------------+-------+-------public|saddress|table|satpublic|sbtsparams|table|satpublic|sbugtrack|table|satpublic|scheckpoint|table|satpublic|sconn_2002_8t_cpos|table|satpublic|sconn_2002_e1_p4_ge|table|satpublic|sconn_backup_tf_file|table|satpublic|sconn_fr1002|table|satpublic|sconn_fr1002_allge|table|satpublic|sconn_nrose|table|satpublic|sconn_s5700|table|satpublic|sconn_testbedname|table|satpublic|sconn_tsr|table|satpublic|sconn_zebos|table|satpublic|scontrollibrary|table|satpublic|sdevice_2002_8t_cpos|table|satpublic|sdevice_2002_e1_p4_ge|table|satpublic|sdevice_backup_tf_file|table|satpublic|sdevice_fr1002|table|satpublic|sdevice_fr1002_allge|table|satpublic|sdevice_nrose|table|satpublic|sdevice_s5700|table|satpublic|sdevice_testbedname|table|satpublic|sdevice_tsr|table|satpublic|sdevice_zebos|table|satpublic|sdeviceparamstyle|table|satpublic|sdevints_2002_8t_cpos|table|satpublic|sdevints_2002_e1_p4_ge|table|satpublic|sdevints_backup_tf_file|table|satpublic|sdevints_fr1002|table|satpublic|sdevints_fr1002_allge|table|satpublic|sdevints_nrose|table|satpublic|sdevints_s5700|table|satpublic|sdevints_testbedname|table|satpublic|sdevints_tsr|table|satpublic|sdevints_zebos|table|satpublic|sdevpms_2002_8t_cpos|table|satpublic|sdevpms_2002_e1_p4_ge|table|satpublic|sdevpms_backup_tf_file|table|satpublic|sdevpms_fr1002|table|satpublic|sdevpms_fr1002_allge|table|satpublic|sdevpms_nrose|table|satpublic|sdevpms_s5700|table|satpublic|sdevpms_testbedname|table|satpublic|sdevpms_tsr|table|satpublic|sdevpms_zebos|table|satpublic|sfirmware|table|satpublic|shc_item|table|satpublic|shc_plugin|table|satpublic|shc_result|table|satpublic|sidmanager|table|satpublic|sjobbed|table|satpublic|sjobhisparams|table|satpublic|sjobhistory|table|satpublic|sjobparams|table|satpublic|sjobparamstyle|table|satpublic|sjobqueue|table|satpublic|sjobresult|table|satpublic|snotifiers|table|satpublic|sorisysconf|table|satpublic|splugin|table|satpublic|splugin_venus|table|satpublic|squeue|table|satpublic|sruntimelib|table|satpublic|sscm|table|satpublic|ssdcrontask|table|satpublic|ssdcrontaskparams|table|satpublic|ssdmessagetype|table|satpublic|ssdprocess|table|satpublic|ssuite|table|satpublic|ssuitecases|table|satpublic|ssysconf|table|satpublic|ssysevents|table|satpublic|ssyspermission|table|satpublic|ssysrole|table|satpublic|stestbed|table|satpublic|stestcaseresult|table|satpublic|stopaddress|table|satpublic|stopconn|table|satpublic|stopdevice|table|satpublic|stopology|table|satpublic|suseraccount|table|satpublic|suserparam|table|satpublic|svariables|table|satpublic|svlanid_2002_8t_cpos|table|satpublic|svlanid_2002_e1_p4_ge|table|satpublic|svlanid_backup_tf_file|table|satpublic|svlanid_fr1002|table|satpublic|svlanid_fr1002_allge|table|satpublic|svlanid_nrose|table|satpublic|svlanid_s5700|table|satpublic|svlanid_testbed|table|satpublic|svlanid_tsr|table|satpublic|svlanid_zebos|table|sat(94rows)可以查看表的內(nèi)容。sat=#select*fromsuseraccount;username|passwd|userlevel|userrole|useremail|telphone|btsuser|btspwd|comment----------------+--------+-----------+----------+--------------------------------+----------+---------+--------+------------------------------------------admin|||ROOT|admin@localhost||||U2lnbWF0aW9uVEYgb25seSBhZG1pbmlzdHJhdG9yhewei|||Advance|hewei@cdforward.office||||liuyifan|||Advance|liuyifan@cdforward.office||||zhuruijie|||Advance|zhuruijie@cdforward.office||||pengqianshun|||Advance|pengqianshun@cdforward.office||||yaolinjiang|||Advance|yaolinjiang@cdforward.office||||zhangzhihong|||Advance|zhangzhihong@cdforward.office||||chengqin|||Advance|chengqin@cdforward.office||||xiaxue|||Advance|xiaxue@cdforward.office||||wulong|||Advance|wulong@cdforward.office||||shangrenshuang|||Advance|shangrenshuang@cdforward.offic||||(11rows)關(guān)于如何使用數(shù)據(jù)庫(kù)PostgreSQL,可以參考<<PostgreSQL新手入門(mén)>>.PostgreSQL新手入門(mén)一、安裝首先,安裝PostgreSQL客戶端。sudoapt-getinstallpostgresql-client然后,安裝PostgreSQL服務(wù)器。sudoapt-getinstallpostgresql正常情況下,安裝完成后,PostgreSQL服務(wù)器會(huì)自動(dòng)在本機(jī)的5432端口開(kāi)啟。如果還想安裝圖形管理界面,可以運(yùn)行下面命令,但是本文不涉及這方面內(nèi)容。sudoapt-getinstallpgadmin3二、添加新用戶和新數(shù)據(jù)庫(kù)初次安裝后,默認(rèn)生成一個(gè)名為postgres的數(shù)據(jù)庫(kù)和一個(gè)名為postgres的數(shù)據(jù)庫(kù)用戶。這里需要注意的是,同時(shí)還生成了一個(gè)名為postgres的Linux系統(tǒng)用戶。下面,我們使用postgres用戶,來(lái)生成其他用戶和新數(shù)據(jù)庫(kù)。好幾種方法可以達(dá)到這個(gè)目的,這里介紹兩種。第一種方法,使用PostgreSQL控制臺(tái)。首先,新建一個(gè)Linux新用戶,可以取你想要的名字,這里為dbuser。sudoadduserdbuser然后,切換到postgres用戶。sudosu-postgres下一步,使用psql命令登錄PostgreSQL控制臺(tái)。psql這時(shí)相當(dāng)于系統(tǒng)用戶postgres以同名數(shù)據(jù)庫(kù)用戶的身份,登錄數(shù)據(jù)庫(kù),這是不用輸入密碼的。如果一切正常,系統(tǒng)提示符會(huì)變?yōu)?postgres=#",表示這時(shí)已經(jīng)進(jìn)入了數(shù)據(jù)庫(kù)控制臺(tái)。以下的命令都在控制臺(tái)內(nèi)完成。第一件事是使用\password命令,為postgres用戶設(shè)置一個(gè)密碼。\passwordpostgres第二件事是創(chuàng)建數(shù)據(jù)庫(kù)用戶dbuser(剛才創(chuàng)建的是Linux系統(tǒng)用戶),并設(shè)置密碼。CREATEUSERdbuserWITHPASSWORD'password';第三件事是創(chuàng)建用戶數(shù)據(jù)庫(kù),這里為exampledb,并指定所有者為dbuser。CREATEDATABASEexampledbOWNERdbuser;第四件事是將exampledb數(shù)據(jù)庫(kù)的所有權(quán)限都賦予dbuser,否則dbuser只能登錄控制臺(tái),沒(méi)有任何數(shù)據(jù)庫(kù)操作權(quán)限。GRANTALLPRIVILEGESONDATABASEexampledbtodbuser;最后,使用\q命令退出控制臺(tái)(也可以直接按ctrl+D)。\q第二種方法,使用shell命令行。添加新用戶和新數(shù)據(jù)庫(kù),除了在PostgreSQL控制臺(tái)內(nèi),還可以在shell命令行下完成。這是因?yàn)镻ostgreSQL提供了命令行程序createuser和createdb。還是以新建用戶dbuser和數(shù)據(jù)庫(kù)exampledb為例。首先,創(chuàng)建數(shù)據(jù)庫(kù)用戶dbuser,并指定其為超級(jí)用戶。sudo-upostgrescreateuser--superuserdbuser然后,登錄數(shù)據(jù)庫(kù)控制臺(tái),設(shè)置dbuser用戶的密碼,完成后退出控制臺(tái)。sudo-upostgrespsql\passworddbuser\q接著,在shell命令行下,創(chuàng)建數(shù)據(jù)庫(kù)exampledb,并指定所有者為dbuser。sudo-upostgrescreatedb-Odbuserexampledb三、登錄數(shù)據(jù)庫(kù)添加新用戶和新數(shù)據(jù)庫(kù)以后,就要以新用戶的名義登錄數(shù)據(jù)庫(kù),這時(shí)使用的是psql命令。psql-Udbuser-dexampledb-h127.0.0.1-p5432上面命令的參數(shù)含義如下:-U指定用戶,-d指定數(shù)據(jù)庫(kù),-h指定服務(wù)器,-p指定端口。輸入上面命令以后,系統(tǒng)會(huì)提示輸入dbuser用戶的密碼。輸入正確,就可以登錄控制臺(tái)了。psql命令存在簡(jiǎn)寫(xiě)形式。如果當(dāng)前Linux系統(tǒng)用戶,同時(shí)也是PostgreSQL用戶,則可以省略用戶名(-U參數(shù)的部分)。舉例來(lái)說(shuō),我的Linux系統(tǒng)用戶名為ruanyf,且PostgreSQL數(shù)據(jù)庫(kù)存在同名用戶,則我以ruanyf身份登錄Linux系統(tǒng)后,可以直接使用下面的命令登錄數(shù)據(jù)庫(kù),且不需要密碼。psqlexampledb此時(shí),如果PostgreSQL內(nèi)部還存在與當(dāng)前系統(tǒng)用戶同名的數(shù)據(jù)庫(kù),則連數(shù)據(jù)庫(kù)名都可以省略。比如,假定存在一個(gè)叫做ruanyf的數(shù)據(jù)庫(kù),則直接鍵入psql就可以登錄該數(shù)據(jù)庫(kù)。psql另外,如果要恢復(fù)外部數(shù)據(jù),可以使用下面的命令。psqlexampledb<exampledb.sql四、控制臺(tái)命令除了前面已經(jīng)用到的\password命令(設(shè)置密碼)和\q命令(退出)以外,控制臺(tái)還提供一系列其他命令。\h:查看SQL命令的解釋,比如\hselect。\?:查看psql命令列表。\l:列出所有數(shù)據(jù)庫(kù)。\c[database_name]:連接其他數(shù)據(jù)庫(kù)。\d:列出當(dāng)前數(shù)據(jù)庫(kù)的所有表格。\d[table_name]:列出某一張表格的結(jié)構(gòu)。\du:列出所有用戶。\e:打開(kāi)文本編輯器。\conninfo:列出當(dāng)前數(shù)據(jù)庫(kù)和連接的信息。五、數(shù)據(jù)庫(kù)操作基本的數(shù)據(jù)庫(kù)操作,就是使用一般的SQL語(yǔ)言。#創(chuàng)建新表
CREATETABLEuser_tbl(nameVARCHAR(20),signup_dateDATE);#插入數(shù)據(jù)
INSERTINTOuser_tbl(name,signup_date)VALUES('張三','2013-12-22');#選擇記錄
SELECT*FROMuser_tbl;#更新數(shù)據(jù)
UPDATEuser_tblsetname='李四'WHEREname='張三';#刪除記錄
DELETEFROMuser_tblWHEREname='李四';#添加欄位
ALTERTABLEuser_tblADDemailVARCHAR(40);#更新結(jié)構(gòu)
ALTERTABLEuser_tblALTERCOLUMNsignup_dateSETNOTNULL;#更名欄位
ALTERTABLEuser_tblRENAMECOLUMNsignup_dateTOsignup;#刪除欄位
ALTERTABLEuser_tblDROPCOLUMNemail;#表格更名
ALTERTABLEuser_tblRENAMETObackup_tbl;#刪除表格
DROPTABLEIFEXISTSbackup_tbl;關(guān)于PostgreSQL的備份與恢復(fù),可以參考<<PostgreSQL備份與恢復(fù)示例>>.PostgreSQL備份與恢復(fù)示例一、簡(jiǎn)介Postgresql的備份分為三種:SQL轉(zhuǎn)儲(chǔ)文件系統(tǒng)級(jí)別備份(冷備份)在線熱備份(歸檔)以下通過(guò)實(shí)例來(lái)講解PostgreSQL的三種備份。
二、SQL轉(zhuǎn)儲(chǔ)(一)pg_dump1,
創(chuàng)建數(shù)據(jù)庫(kù)createdbpg2,
連入數(shù)據(jù)庫(kù)pgpsqlpg3,
創(chuàng)建測(cè)試表,插入數(shù)據(jù)pg=#createtablepg_test(aint);pg=#insertintopg_test(a)values(1);pg=#insertintopg_test(a)values(2);4,
查看數(shù)據(jù)pg=#select*fromtb;a---12(2rows)5,備份pg_dumppg>/usr/local/pgsql/backup/pg.dmp6,
刪除數(shù)據(jù)庫(kù)pgdropdbpg7,
創(chuàng)建新數(shù)據(jù)庫(kù)(恢復(fù)之前需創(chuàng)建數(shù)據(jù)庫(kù))createdbpg8,
恢復(fù)數(shù)據(jù)psqlpb</usr/local/pgsql/backup/pg.dmp9,
查看數(shù)據(jù)是否回復(fù)pg=#select*fromtb;a---12(2rows)
至此,數(shù)據(jù)已成功恢復(fù)!
注:pg_dump可以對(duì)針對(duì)單表或者多表進(jìn)行備份如:pg_dumpdatabasename–ttablename1–ttablename2>filename(二)pg_dumpall pg_dump只能備份單個(gè)數(shù)據(jù)庫(kù),而且恢復(fù)的時(shí)候需要?jiǎng)?chuàng)建空數(shù)據(jù)庫(kù)。pg_dumpall可以備份所有數(shù)據(jù)庫(kù),并且備份角色、表空間。1,
創(chuàng)建數(shù)據(jù)庫(kù)createdbpg1createdbpg22,
pg1中創(chuàng)建表并插入數(shù)據(jù)psqlpg1pg=#createtabletb1(aint)pg=#insertintotb1(a)values(1);3,
pg2中創(chuàng)建表并插入數(shù)據(jù)psqlpg2pg=#createtabletb2(aint)pg=#insertintotb2(a)values(2);4,
備份數(shù)據(jù)庫(kù)pg_dumpall>/usr/local/pgsql/backup/pg_all.dmp5,
刪除數(shù)據(jù)庫(kù)dropdbpg1dropdbpg26,
恢復(fù)數(shù)據(jù)庫(kù)psql–f/usr/local/pgsql/backup/pg_all.dmppostgres 可以指定任何數(shù)據(jù)庫(kù)名,如果恢復(fù)到一個(gè)空的集群中,通常使用postgres數(shù)據(jù)庫(kù)7,
查看數(shù)據(jù)庫(kù)pg1是否恢復(fù)psqlpg1pg=#select*fromtb1;a---1(1rows)8,查看數(shù)據(jù)庫(kù)pg2是否恢復(fù)psqlpg2pg=#select*fromtb2;a---2(1rows)
至此,數(shù)據(jù)已成功恢復(fù)!
(三)備份壓縮與分割壓縮與分割的原理都是利用Linux的管線(PIPE)命令,不再進(jìn)行試驗(yàn)。1)壓縮備份:pg_dumpdbname|gzip>filename.gz恢復(fù)gunzip-cfilename.gz|psqldbname
2)分割備份:pg_dumpdbname|split-b1m-filename恢復(fù)catfilename*|psqldbname 三、文件系統(tǒng)級(jí)別備份(冷備份)文件系統(tǒng)級(jí)別的備份是冷備份,需要停止數(shù)據(jù)庫(kù)。1,
停止數(shù)據(jù)庫(kù)pg_ctl–D/usr/local/pgsql/datastop2,
備份數(shù)據(jù)庫(kù)tar–jcv–f/usr/local/pgsql/backup/filesystem.tar.bz2/usr/local/pgsql/data/3,
刪除/usr/local/pgsql/data/目錄rm–r/usr/local/pgsql/data/4,
解壓備份文件到原目錄tar–jxv–f/usr/local/pgsql/backup/filesystem.tar.bz2–C/5,
啟動(dòng)數(shù)據(jù)庫(kù)pg_ctl–D/usr/local/pgsql/datastart6,
查看數(shù)據(jù)庫(kù)pg1是否恢復(fù)psqlpg1pg=#select*fromtb1;a---1(1rows)7,查看數(shù)據(jù)庫(kù)pg2是否恢復(fù)psqlpg2pg=#select*fromtb2;a---2(1rows)
至此,數(shù)據(jù)已成功恢復(fù)!
四、
在線熱備份(歸檔)(一)備份1,配置歸檔模式配置歸檔需要編輯postgresql.conf文件,默認(rèn)為與/usr/local/pgsql/data/目錄下vim/usr/local/pgsql/data/postgesql.confa
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年幼兒園小班國(guó)慶節(jié)主題慶祝祖國(guó)媽媽生日標(biāo)準(zhǔn)教案
- 二零二五年度房屋維修資金頂賬退還協(xié)議書(shū)
- 二零二五年度景區(qū)合作合同-景區(qū)旅游電子商務(wù)平臺(tái)合作協(xié)議
- 二零二五年度機(jī)場(chǎng)充電樁場(chǎng)地租賃及旅客服務(wù)協(xié)議
- 二零二五年度生物制藥公司股權(quán)轉(zhuǎn)讓及創(chuàng)新藥物開(kāi)發(fā)合同
- 二零二五年度文化娛樂(lè)節(jié)目編排與市場(chǎng)推廣協(xié)議
- 二零二五年度臨時(shí)撫養(yǎng)權(quán)變更協(xié)議范本
- 二零二五年度商業(yè)辦公空間租賃免責(zé)合同
- 中藥貯藏與保管(中藥炮制技術(shù)課件)
- 二零二五年度民宿合作社民宿與旅行社合作共贏協(xié)議
- 公路工程檢測(cè)技術(shù) 課件 項(xiàng)目1 試驗(yàn)檢測(cè)知識(shí)
- 寺廟線上運(yùn)營(yíng)策劃方案
- 動(dòng)態(tài)公路車輛自動(dòng)衡器
- 委托收款三方協(xié)議書(shū)
- 電路邱關(guān)源版第10章
- 綠植租擺服務(wù)投標(biāo)方案(技術(shù)方案)
- 2020新譯林版高中英語(yǔ)全七冊(cè)單詞表(必修一~選擇性必修四)
- 七年級(jí)上冊(cè)生物期末測(cè)試卷(含答案)
- 路基分層-表格-
- 離婚協(xié)議書(shū)電子版下載
- 汽車法規(guī)第一章
評(píng)論
0/150
提交評(píng)論