PostgreSQL

PostgreSQL 安裝

侯清慈(AlexHou) 2019/11/27 09:26:09
3127

什麼是PostgreSQL?

PostgreSQL 為開源的關聯式資料庫伺服器(DBMS),授權模式採取 BSD 授權許可;其最大的好處是可以用於商業營運時,修改成為自己產品的一部分而無須支付任何費用,也不必擔心需要將修改過後的原始碼公開;不同於GPL授權軟體都得公開原始碼所需面對的問題。

PostgreSQL具有商業資料庫絕大部分的功能,對於中小企業的應用十分的足夠。相較於 MySQL 這開放的資料庫系統,他有更完整的關聯式資料庫功能及優秀的穩定性。PostgreSQL是一個強大的資料庫引擎:交易、觸發、內存程序等完整的關聯式資料庫機制,可提供開發人員在開發過程中更方便的操作。   

 

PostgreSQL 資料庫可以有多大?

參考:https://faq.postgresql.tw/postgresql-zi-liao-ku-ke-yi-you-duo-da

 

PostgreSQL跟其他DBMS的比較

PostgreSQL vs MySQL vs SQL Server vs Oracle

對於哪個 SQL 資料庫系統比較優秀,網路上有不同派系的粉絲建議/爭議,在這邊就不特別比較啦!

以下連結來自PostgreSQL討論區,純參考哦!

https://faq.postgresql.tw/postgresql-vs-mysql-vs-sql-server-vs-oracle

 

PostgreSQL 的安裝

安裝環境:

OSRedHat 7.6

RAM: 4G

Disk: 20G+ (依需求增加)

版本:postgresql 9.6

 

PostgreSQL 的套件安裝

網路安裝方式:

1.Install the repository RPM

   # yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

 

2.Install the Postgresql packages

 

3.initialize the database and enable automatic start

# /usr/pgsql-9.6/bin/postgresql96-setup initdb
# systemctl enable postgresql-9.6
# systemctl start postgresql-9.6

 

PostgreSQL會在安裝過程中建立postgres user, postgresql程式在linux 上會依此使用者帳戶執行。

postgres 使用者的Home目錄在/var/lib/pgsql/

在/var/lib/pgsql/9.6/data, 包涵了所有 postgresql 所有需要用到的檔案內容:即所有相關的設定檔、紀錄檔及資料庫檔案都在裡面。

 

以上 , PostgreSQL就安裝完成了! 是多麼簡單快速的安裝啊!

這樣就可以交待了嗎?

當然不是啦、請再繼續看下文說明!

 

 

PostgreSQL離線安裝方式:

在專案進行中,常會因需求/管理來歸劃檔案的位置,PostgreSQL的初始檔案安排位置,常不是專案所需求的!

也因安全考量,需要在客戶內網進行安裝!

以下以離線安裝+異動所有相關的設定檔、紀錄檔及資料庫檔案來示範說明:

 

1. 取得安裝檔

    巧婦難為無米之炊!

    得先要在有網路的環境download 安裝檔!

    # wget https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/postgresql96-9.6.15-1PGDG.rhel7.x86_64.rpm

    # wget https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/postgresql96-libs-9.6.15-1PGDG.rhel7.x86_64.rpm

    # wget https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/postgresql96-server-9.6.15-1PGDG.rhel7.x86_64.rpm

    使用yum指令來安裝postgresql

    # yum install *.rpm

 

2. 調整目錄架構;依需求建立目錄

Ex:

    # mkdir -p /opt/thinkpower/postgresql/pgdata

    # mkdir -p /opt/thinkpower/postgresql/ar_log

    # mkdir -p /opt/thinkpower/postgresql/pgbackup

    # mkdir -p /opt/thinkpower/postgresql/pg_log

 

3. 調整目錄權限

    # chown -R postgres:postgres /opt/thinkpower/postgresql

    # chmod -R 700 /opt/thinkpower/postgresql/pgdata

 

4. Database初始化

此規劃PostgreSQLdatabase/設定檔位置於/opt/thinkpower/postgresql/pgdata

 

    # su - postgres

    # /usr/pgsql-9.6/bin/initdb --no-locale -U postgres -E utf8 -D /opt/thinkpower/postgresql/pgdata/ -W

5. 調整systemd for PostgreSQL

    # su -

    # cd /usr/lib/systemd/system/

 

    # vi postgresql-9.6.service

    將行31調整為已修改的路徑

    Environment=PGDATA=/var/lib/pgsql/9.6/data/

    修改為

    Environment=PGDATA=/opt/thinkpower/postgresql/pgdata

 

6. Reload systemctl daemon

    # systemctl daemon-reload

 

7. 修改postgresql.conf

    # vi postgresql.conf

    調整log路徑:

    log_directory = '/opt/thinkpower/postgresql/pg_log'

 

    其他設定如有需求再依規劃內容調整!

    如postgres程式使用的記憶體大小:shared_buffers 參數

    連線數調整:max_connections

    連線Port

    …等。

 

7. 調整pg_hba.conf

    此情境為本機登錄為信任模式,其他Host連線需帳密權限

 

    pg_hba.conf

    # "local" is for Unix domain socket connections only

    local   all             all                                          trust

    # IPv4 local connections:

    host    all             all             127.0.0.1/32          md5

    host    all             all             192.168.22.0/24    md5

    # IPv6 local connections:

    host    all             all             ::1/128                   trust

 

登入PostgreSQL

    使用postgres user login PostgreSQL.

    #psql -U postgres 

後續就可依專案需求來建立userdatabaseDDL, DML

 

 Ex:

建DB user

 

CREATE ROLE APUSER WITH

         LOGIN

         SUPERUSER

         CREATEDB

         CREATEROLE

         INHERIT

         NOREPLICATION

         CONNECTION LIMIT -1

         PASSWORD 'xxxxxxxxx';

 

 

 

建database

 

CREATE DATABASE TPDB

    WITH

    OWNER = APUSER

    ENCODING = 'UTF8'

CONNECTION LIMIT = -1;

 

參考資料:

PostgreSQL 官方手冊

https://docs.postgresql.tw/qian-yan

 

PostgreSQL常見問題

https://faq.postgresql.tw/

 

PostgreSQL 安裝(官網)

https://www.postgresql.org/download/linux/redhat/

 

  

 

 

侯清慈(AlexHou)