<label id="60jrf"><meter id="60jrf"><bdo id="60jrf"></bdo></meter></label>
    <thead id="60jrf"><optgroup id="60jrf"></optgroup></thead>

      <span id="60jrf"><optgroup id="60jrf"><center id="60jrf"></center></optgroup></span>
      1. Ubuntu系統上安裝和配置PostgreSQL數據庫流程
        作者:admin | 時間:2020-11-18 21:05:36

        Ubuntu上安裝PostgreSQL

        PostgreSQL在Ubuntu主存儲庫中可用,但是,與許多其他開發工具一樣,它可能不是最新版本。

        首先使用終端中的apt命令檢查Ubuntu存儲庫中可用的PostgreSQL版本:

        apt show postgresql

        在我的Ubuntu 18.04中,它顯示PostgreSQL的可用版本是版本10(10+190表示版本10),而PostgreSQL 11版本已經發布,參考在Ubuntu 18.04/Ubuntu 16.04上安裝PostgreSQL 11的說明:

        Package: postgresql

        Version: 10+190

        Priority: optional

        Section: database

        Source: postgresql-common (190)

        Origin: Ubuntu

        根據這些信息,你可以考慮是否要安裝Ubuntu提供的版本,或者你想獲得最新發布的PostgreSQL版本,下面介紹這兩種方法。

        方法1:從Ubuntu存儲庫安裝PostgreSQL

        在終端中,使用以下命令安裝PostgreSQL:

        sudo apt update

        sudo apt install postgresql postgresql-contrib

        介紹:什么是postgresql-contrib?

        postgresql-contrib或contrib包包含一些不屬于核心PostgreSQL包的其他實用程序和功能,在大多數情況下,最好將contrib包與PostgreSQL核心一起安裝。

        方法2:在Ubuntu中安裝PostgreSQL的最新版本11

        要安裝PostgreSQL 11,需要在sources.list中添加官方PostgreSQL存儲庫,添加其證書,然后從那里安裝它,只需按照以下步驟操作。

        首先添加GPG密鑰:

        wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

        現在使用以下命令添加存儲庫,如果你使用的是Linux Mint,則必須手動替換Mint版本所基于的Ubuntu版本的`lsb_release -cs`。

        sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

        現在一切都準備好了,使用以下命令安裝PostgreSQL:

        sudo apt update

        sudo apt install postgresql postgresql-contrib

        介紹:PostgreSQL GUI應用程序:

        你還可以安裝用于管理PostgreSQL數據庫的GUI應用程序(pgAdmin):

        sudo apt install pgadmin4

        參考:Ubuntu/Debian 上安裝pgAdmin 4的方法。

         

        配置PostgreSQL

        可以通過執行以下命令檢查PostgreSQL是否正在運行:

        service postgresql status

        通過service命令,還可以啟動,停止或重新啟動postgresql,鍵入服務postgresql并按Enter鍵應輸出所有選項,現在,進入用戶。

        默認情況下,PostgreSQL創建一個具有所有權限的特殊用戶postgres,要實際使用PostgreSQL,必須先登錄該帳戶:

        sudo su postgres

        你的提示應更改為類似于:

        postgres@ubuntu-VirtualBox:/home/ubuntu$

        現在,使用實用程序psql運行PostgreSQL Shell:

        psql

        系統會提示你:

        postgress=#

        你可以輸入q來退出和?求助。

        要查看所有現有表,請輸入:

        l

        輸出看起來與此類似(點擊鍵q退出此視圖):

        ubuntu.jpg


        使用du,可以顯示PostgreSQL用戶:

        postgres.jpg


        你可以使用以下命令更改任何用戶(包括postgres)的密碼:

        ALTER USER postgres WITH PASSWORD 'my_password';

        注意:將postgres替換為用戶名,將my_password替換為所需密碼。

        建議你創建另一個用戶(使用默認的postgres用戶是不好的做法),為此,請使用以下命令:

        CREATE USER my_user WITH PASSWORD 'my_password';

        如果你運行du,你會看到my_user還沒有屬性,讓我們為它添加超級用戶:

        ALTER USER my_user WITH SUPERUSER;

        可以刪除用戶:

        DROP USER my_user;

        要以其他用戶身份登錄,請退出提示符(q),然后使用以下命令:

        psql -U my_user

        你可以使用-d參數直接連接到數據庫:

        psql -U my_user -d my_db

        你應該像其他現有用戶一樣調用PostgreSQL用戶,例如我用的是ubuntu,下面要登錄,運行:

        psql -U ubuntu -d postgres

        注意:你必須指定一個數據庫(默認情況下,它會嘗試將你連接到與你登錄的用戶名稱相同的數據庫)。

        如果有錯誤:

        psql: FATAL:  Peer authentication failed for user "my_user"

        確保你以正確的用戶身份登錄并使用管理員權限編輯/etc/postgresql/11/main/pg_hba.conf:

        sudo vim /etc/postgresql/11/main/pg_hba.conf

        注意:將11替換為你的版本(例如10)。

        在這里,替換行:

        local all  postgres  peer

        With:

        local all  postgres  md5

        然后重啟PostgreSQL:

        sudo service postgresql restart

        注:使用PostgreSQL與使用任何其他SQL類型數據庫相同。


        資訊內容

        誠信為本,卓越品質,做行業領跑者