Ubuntu下postgres的一些操作總結

來源:互聯網
上載者:User

標籤:postgresql

Ubuntu下postgres的一些操作總結


1. 基本操作命令

  • 安裝完成後,PostgreSQL預設建立了名為postgres資料庫使用者賬戶,其與MySQL的root以及SQLServer的sa賬戶一樣,是超級管理員賬戶,但與MySQL不一樣的是,PostgreSQL還建立了名為postgres的Unix系統賬戶。

  • 和SQLServer的master資料庫一樣,PostgreSQL的預設資料庫是template1,你可以用命令列管理工具psql來管理它,當然前提是要切換到postgres的系統賬戶。

  • 在其中的常用命令列命令如下:

createdb dbname:建立資料庫
dropdb dbname: 刪除資料庫
createuser [--superuser] username:建立使用者
dropuserusername:               刪除使用者
  • 在互動式環境下常用到的命令:

\du:列出當前所有的使用者資訊
\l: 列出當前所有的資料庫資訊
\q: 退出
\password username:修改指定的user的密碼
\dn:查看shema
\i:命令從指定的檔案中讀取命令

2. 使用shell命令列方式添加使用者和建立資料庫


PostgreSQL提供了命令列程式createuser和createdb,可以使用shell命令列方式添加使用者和建立資料庫。以建立使用者dbuser和資料庫exampledb為例。


  • 首先,建立資料庫使用者dbuser,並指定其為超級使用者
    sudo -u postgres createuser --superuser dbuser

  • 然後,登入資料庫控制台,設定dbuser使用者的密碼,完成後退出控制台。
    sudo -u postgres psql
    \password dbuser
    \q

  • 接著,在shell命令列下,建立資料庫exampledb,並指定所有者為dbuser。
    sudo -u postgres createdb -O dbuser exampledbshell命令列登入資料庫

  • 同樣以上述例子中的使用者和資料庫為例,使用新使用者的名義登入資料庫
    psql -U dbuser -d exampledb -h 127.0.0.1 -p 5432
    上面命令的參數含義如下:-U指定使用者,-d指定資料庫,-h指定伺服器,-p指定連接埠。輸入上面命令以後,系統會提示輸入dbuser使用者的密碼。輸入正確,就可以登入控制台。

3.Ubuntu下postgresql的服務開啟關閉方式使用如下命令:

  • Usage: /etc/init.d/postgresql{start|stop|restart|reload|force-reload|status} [version ..]

4. 串連資料庫時出現一些認證情況的解決辦法:

  • 如果出現下面的錯誤:

    psql: FATAL: Ident authentication failed for user"mypguser"

    請編輯你的pg_hba.conf,這個檔案一般位於/etc/postgresql/X.Y/main/pg_hba.conf,X.Y是你的PostgreSQL的版本號碼,將下面行的peer改為trust:

local   all    all    trust     # replace ident or peer with trust

 

  • 如果出現下面的錯誤:

    psql: FATAL: Peer authentication failed foruser "mypguser"

    請仍然修改pg_hba.conf檔案,該下面行的peer為md5:

local   all    all    md5       # replace peer with md5

完成上面的修改後請重新載入postgresql:

/etc/init.d/postgresql reload



本文出自 “斜視天花板” 部落格,請務必保留此出處http://lemidi.blog.51cto.com/8601832/1535349

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.