<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系統(tǒng)上安裝和配置PostgreSQL數(shù)據(jù)庫(kù)流程
        作者:admin | 時(shí)間:2020-11-18 21:05:36

        Ubuntu上安裝PostgreSQL

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

        首先使用終端中的apt命令檢查Ubuntu存儲(chǔ)庫(kù)中可用的PostgreSQL版本:

        apt show postgresql

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

        Package: postgresql

        Version: 10+190

        Priority: optional

        Section: database

        Source: postgresql-common (190)

        Origin: Ubuntu

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

        方法1:從Ubuntu存儲(chǔ)庫(kù)安裝PostgreSQL

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

        sudo apt update

        sudo apt install postgresql postgresql-contrib

        介紹:什么是postgresql-contrib?

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

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

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

        首先添加GPG密鑰:

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

        現(xiàn)在使用以下命令添加存儲(chǔ)庫(kù),如果你使用的是Linux Mint,則必須手動(dòng)替換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'

        現(xiàn)在一切都準(zhǔn)備好了,使用以下命令安裝PostgreSQL:

        sudo apt update

        sudo apt install postgresql postgresql-contrib

        介紹:PostgreSQL GUI應(yīng)用程序:

        你還可以安裝用于管理PostgreSQL數(shù)據(jù)庫(kù)的GUI應(yīng)用程序(pgAdmin):

        sudo apt install pgadmin4

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

         

        配置PostgreSQL

        可以通過(guò)執(zhí)行以下命令檢查PostgreSQL是否正在運(yùn)行:

        service postgresql status

        通過(guò)service命令,還可以啟動(dòng),停止或重新啟動(dòng)postgresql,鍵入服務(wù)postgresql并按Enter鍵應(yīng)輸出所有選項(xiàng),現(xiàn)在,進(jìn)入用戶。

        默認(rèn)情況下,PostgreSQL創(chuàng)建一個(gè)具有所有權(quán)限的特殊用戶postgres,要實(shí)際使用PostgreSQL,必須先登錄該帳戶:

        sudo su postgres

        你的提示應(yīng)更改為類似于:

        postgres@ubuntu-VirtualBox:/home/ubuntu$

        現(xiàn)在,使用實(shí)用程序psql運(yùn)行PostgreSQL Shell:

        psql

        系統(tǒng)會(huì)提示你:

        postgress=#

        你可以輸入q來(lái)退出和?求助。

        要查看所有現(xiàn)有表,請(qǐng)輸入:

        l

        輸出看起來(lái)與此類似(點(diǎn)擊鍵q退出此視圖):

        ubuntu.jpg


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

        postgres.jpg


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

        ALTER USER postgres WITH PASSWORD 'my_password';

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

        建議你創(chuàng)建另一個(gè)用戶(使用默認(rèn)的postgres用戶是不好的做法),為此,請(qǐng)使用以下命令:

        CREATE USER my_user WITH PASSWORD 'my_password';

        如果你運(yùn)行du,你會(huì)看到my_user還沒(méi)有屬性,讓我們?yōu)樗砑映?jí)用戶:

        ALTER USER my_user WITH SUPERUSER;

        可以刪除用戶:

        DROP USER my_user;

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

        psql -U my_user

        你可以使用-d參數(shù)直接連接到數(shù)據(jù)庫(kù):

        psql -U my_user -d my_db

        你應(yīng)該像其他現(xiàn)有用戶一樣調(diào)用PostgreSQL用戶,例如我用的是ubuntu,下面要登錄,運(yùn)行:

        psql -U ubuntu -d postgres

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

        如果有錯(cuò)誤:

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

        確保你以正確的用戶身份登錄并使用管理員權(quán)限編輯/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類型數(shù)據(jù)庫(kù)相同。


        資訊內(nèi)容

        誠(chéng)信為本,卓越品質(zhì),做行業(yè)領(lǐng)跑者