Ubuntu 下編譯安裝 PostgreSQL


前言

   最近有同學問到在 Ubuntu 下如何編譯安裝 PostgreSQL。這次內容就來說一下如何在 Ubuntu 操作系統中安裝 PostgreSQL 數據庫。那么,安裝 PostgreSQL 對於 DBA 來說是再簡單不過的事兒了,我們知道 PostgreSQL 在 Redhat 家族系統中有三種安裝方式,分別是源碼安裝,rpm 方式安裝和yum 方式安裝,當然,rpm 安裝和 yum 安裝可以當做是同類型的安裝,除此之外,就是使用已經編譯好的二進制安裝。那么對於Ubuntu操作系統來說,除了一鍵化使用 apt-get install 來安裝和 redhat 家族安裝不同以外,源碼安裝和二進制安裝都一樣。但如果使用源碼安裝,需要解決一些依賴。本文就是在最新版本的 Ubuntu 服務器操作系統上安裝 PostgreSQL。

 

NOTE:當前 Ubuntu 操作系統上已經集成了最新的 PostgreSQL 安裝包,版本為 12.9 版本

 

一 、 使用 apt-get 安裝

   apt-get 命令為 Ubuntu 系統管理軟件的命令,利用該命令可以管理,移除,清空,檢查等在 Ubuntu 上安裝的軟件。

1. 操作系統版本

操作系統為 Ubuntu 20.04.3 服務器操作系統

root@developer:~# lsb_release -a
No LSB modules are available.
Distributor ID:Ubuntu
Description:Ubuntu 20.04.3 LTS
Release:20.04
Codename:focal

2. 查看 Ubuntu 上可用的 PostgreSQL 包 

  通過 apt list 可以列出 PostgreSQL 相關的軟件包

root@developer:~# apt list | grep -w  postgresql-12 | tail -1

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

postgresql-12/focal-updates,focal-security 12.9-0ubuntu0.20.04.1 amd64

3. 執行安裝

postgresql-12/focal-updates,focal-security 12.9-0ubuntu0.20.04.1 amd64
        root@developer:~# apt-get install postgresql-12 -y 
        Reading package lists... Done
        Building dependency tree       
        Reading state information... Done
        The following packages were automatically installed and are no longer required:
          libpython2.7 libpython2.7-dev libpython2.7-minimal libpython2.7-stdlib
        Use 'apt autoremove' to remove them.
        The following NEW packages will be installed:
          postgresql-12
        0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded.
        Need to get 0 B/13.5 MB of archives.
        After this operation, 41.1 MB of additional disk space will be used.
        Preconfiguring packages ...
        Selecting previously unselected package postgresql-12.
        (Reading database ... 158185 files and directories currently installed.)
        Preparing to unpack .../postgresql-12_12.9-0ubuntu0.20.04.1_amd64.deb ...
        Unpacking postgresql-12 (12.9-0ubuntu0.20.04.1) ...
        Setting up postgresql-12 (12.9-0ubuntu0.20.04.1) ...
        Creating new PostgreSQL cluster 12/main ...
        /usr/lib/postgresql/12/bin/initdb -D /var/lib/postgresql/12/main --auth-local peer --auth-host md5
        The files belonging to this database system will be owned by user "postgres".
        This user must also own the server process.

        The database cluster will be initialized with locale "en_US.UTF-8".
        The default database encoding has accordingly been set to "UTF8".
        The default text search configuration will be set to "english".

        Data page checksums are disabled.

        fixing permissions on existing directory /var/lib/postgresql/12/main ... ok
        creating subdirectories ... ok
        selecting dynamic shared memory implementation ... posix
        selecting default max_connections ... 100
        selecting default shared_buffers ... 128MB
        selecting default time zone ... Etc/UTC
        creating configuration files ... ok
        running bootstrap script ... ok
        performing post-bootstrap initialization ... ok
        syncing data to disk ... ok

        Success. You can now start the database server using:

            pg_ctlcluster 12 main start

        Ver Cluster Port Status Owner    Data directory              Log file
        12  main    5432 down   postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
        update-alternatives: using /usr/share/postgresql/12/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
        Processing triggers for postgresql-common (214ubuntu0.1) ...
        Building PostgreSQL dictionaries from installed myspell/hunspell packages...
          en_us
        Removing obsolete dictionary files:

4. 根據上面輸出,啟動 PostgreSQL 數據庫集簇

root@developer:~# pg_ctlcluster 12 main start

這里需要注意一下的是,Ubuntu 版本中自帶的 PostgreSQL 數據庫版本對於數據庫集簇管理命令封裝為 pg_ctlcluster 命令。因此,該命令可以執行和 pg_ctl 類似的一些動作,如啟動、停止、重啟、加載等。

5. 檢查進程

root@developer:~# ps -ef |grep postgres
postgres   69578       1  0 07:54 ?        00:00:00 /usr/lib/postgresql/12/bin/postgres -D /var/lib/postgresql/12/main -c config_file=/etc/postgresql/12/main/postgresql.conf
postgres   69580   69578  0 07:54 ?        00:00:00 postgres: 12/main: checkpointer   
postgres   69581   69578  0 07:54 ?        00:00:00 postgres: 12/main: background writer   
postgres   69582   69578  0 07:54 ?        00:00:00 postgres: 12/main: walwriter   
postgres   69583   69578  0 07:54 ?        00:00:00 postgres: 12/main: autovacuum launcher   
postgres   69584   69578  0 07:54 ?        00:00:00 postgres: 12/main: stats collector   
postgres   69585   69578  0 07:54 ?        00:00:00 postgres: 12/main: logical replication launcher

6. 檢查端口

root@developer:~# netstat -anlp | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      69578/postgres      
unix  2      [ ACC ]     STREAM     LISTENING     246072   69578/postgres       /var/run/postgresql/.s.PGSQL.5432

7. 登錄數據庫

root@developer:~# psql -U postgres -d postgres -p 5432
psql: error: FATAL:  Peer authentication failed for user "postgres"

這里在登錄數據庫的時候報對等認證錯誤,那么我們知道連接認證都是基於 pg_hba.conf 條目來配置的,因此該報錯需要修改 pg_hba.conf 配置文件。

  修改配置文件需要知道使用 apt-get 將文件都分發到哪個目錄才可以。在 Ubuntu 系統中,通過 whereis 可找到軟件安裝在哪里,如下,PostgreSQL 軟件安裝的位置

root@developer:~# whereis  -u postgresql
postgresql: /usr/lib/postgresql /etc/postgresql /usr/share/postgresql

上面輸出可以看到,軟件都被安裝到上面三個目錄,當然,可執行程序被安裝到 /usr/bin 目錄下,如下

root@developer:~# cd /usr/bin/
root@developer:/usr/bin# ls -l pg*
lrwxrwxrwx 1 root root    37 Aug 24  2020 pg_archivecleanup -> ../share/postgresql-common/pg_wrapper
lrwxrwxrwx 1 root root    37 Aug 24  2020 pg_basebackup -> ../share/postgresql-common/pg_wrapper
lrwxrwxrwx 1 root root    37 Aug 24  2020 pgbench -> ../share/postgresql-common/pg_wrapper
-rwxr-xr-x 1 root root  9707 Aug 24  2020 pg_buildext
-rwxr-xr-x 1 root root  1229 Aug 24  2020 pg_config
-rwxr-xr-x 1 root root  6262 Aug 24  2020 pg_conftool
-rwxr-xr-x 1 root root 34684 Aug 24  2020 pg_createcluster
-rwxr-xr-x 1 root root 23919 Aug 24  2020 pg_ctlcluster
-rwxr-xr-x 1 root root  7603 Aug 24  2020 pg_dropcluster
lrwxrwxrwx 1 root root    37 Aug 24  2020 pg_dump -> ../share/postgresql-common/pg_wrapper
lrwxrwxrwx 1 root root    37 Aug 24  2020 pg_dumpall -> ../share/postgresql-common/pg_wrapper
lrwxrwxrwx 1 root root    37 Aug 24  2020 pg_isready -> ../share/postgresql-common/pg_wrapper
-rwxr-xr-x 1 root root  5268 Aug 24  2020 pg_lsclusters
lrwxrwxrwx 1 root root    37 Aug 24  2020 pg_receivewal -> ../share/postgresql-common/pg_wrapper
lrwxrwxrwx 1 root root    37 Aug 24  2020 pg_receivexlog -> ../share/postgresql-common/pg_wrapper
lrwxrwxrwx 1 root root    37 Aug 24  2020 pg_recvlogical -> ../share/postgresql-common/pg_wrapper
-rwxr-xr-x 1 root root  5887 Aug 24  2020 pg_renamecluster
-rwxr-xr-x 1 root root 30968 Sep  9 12:59 pgrep
lrwxrwxrwx 1 root root    37 Aug 24  2020 pg_restore -> ../share/postgresql-common/pg_wrapper
-rwxr-xr-x 1 root root 33434 Aug 24  2020 pg_upgradecluster
-rwxr-xr-x 1 root root  7859 Aug 24  2020 pg_virtualenv

修改 pg_hba.conf 文件通過 unix socket 認證方式為 trust

root@developer:~# cd /etc/postgresql/12/main/
root@developer:/etc/postgresql/12/main# vi pg_hba.conf 
root@developer:/etc/postgresql/12/main# cat pg_hba.conf | grep trust
# METHOD can be "trust", "reject", "md5", "password", "scram-sha-256",
local   all             postgres                                trust

再次嘗試連接,連接前需要從新加載基於主機的配置文件

root@developer:~# pg_ctlcluster reload 12 main
root@developer:~# psql -U postgres -d postgres -p 5432
psql (12.9 (Ubuntu 12.9-0ubuntu0.20.04.1))
Type "help" for help.


postgres=# 

可以看到數據庫版本為 12.9 版本。

8. 使用系統命令管理數據庫實例

postgresql.service 系統守護文件和 redhat 家族系統一樣,依然存放在 /usr/lib/systemd/system 目錄下

root@developer:/usr/lib/systemd/system# pwd
/usr/lib/systemd/system
root@developer:/usr/lib/systemd/system# ls postgresql.service 
postgresql.service

那么使用 apt-get 安裝的 PostgreSQL 數據庫也可以使用 systemctl 命令進行管理和維護

二、 使用源碼編譯安裝

1. 上傳安裝包到 /opt/software 目錄並解壓

root@developer:~# cd /opt/software/
root@developer:/opt/software# ls
postgresql-13.5.tar.bz2
root@developer:/opt/software# tar -jxf postgresql-13.5.tar.bz2 
root@developer:/opt/software# ls
postgresql-13.5  postgresql-13.5.tar.bz2

2. 安裝必要的依賴

apt-get install -y systemtap-sdt-dev
apt-get install -y libicu-dev
apt-get install -y libreadline-dev
apt-get install -y zlib1g-dev
apt-get install -y libssl-dev
apt-get install -y libpam-dev
apt-get install -y libxml2-dev
apt-get install -y libxslt-dev
apt-get install -y libldap-dev
apt-get install -y libsystemd-dev
apt-get install -y getext
apt-get install -y tcl-dev
apt-get install -y libpython3-dev
apt-get install -y libperl-dev

3. 切換到數據庫軟件解壓目錄,並執行編譯前配置

切換目錄

root@developer:~# cd /opt/software/postgresql-13.5/
root@developer:/opt/software/postgresql-13.5# 

執行編譯前檢查

export PREFIX="/usr/local/pg13"
export PGPORT=10000
./configure \
--prefix=${PREFIX} \
--exec-prefix=${PREFIX}/pgsql \
--bindir=${PREFIX}/pgsql/bin \
--sysconfdir=${PREFIX}/etc \
--libdir=${PREFIX}/pgsql/lib \
--includedir=${PREFIX}/include \
--datarootdir=${PREFIX}/share \
--localedir=${PREFIX}/locale \
--mandir=${PREFIX}/locale/man \
--docdir=${PREFIX}/locale/doc \
--htmldir=${PREFIX}/locale/html \
--enable-nls='en_US zh_CN' \
--with-perl \
--with-python \
--with-tcl \
--with-icu \
--with-openssl \
--with-ldap \
--with-pam \
--with-systemd \
--with-libxml \
--with-libxslt \
--with-readline \
--with-zlib \
--with-pgport=${PGPORT}

4. 執行編譯和安裝命令

root@developer:/opt/software/postgresql-13.5# make world -j8 && make install-world -j8

編譯安裝的位置如下:

root@developer:/usr/local/pg13# pwd
/usr/local/pg13
root@developer:/usr/local/pg13# ls
include  locale  pgsql  share

5. 創建操作系統用戶和數據庫集簇存放目錄

創建 postgres 用戶

root@developer:~# useradd -u 2000 -c "PostgreSQL db user" -b /home -m -k /etc/skel -s /bin/bash  postgres
root@developer:~# passwd postgres
New password: 
Retype new password: 
passwd: password updated successfully

創建數據庫集簇存放目錄 /data/pg13/pgdata

root@developer:~# mkdir -p /data/pg13/pgdata

授權

root@developer:~# chown postgres.postgres -R /data

6. 切換到 postgres 用戶配置環境變量

root@developer:~# su - postgres
    postgres@developer:~$ vi .bashrc 
    postgres@developer:~$ tail -4 .bashrc 
    export PGHOME=/usr/local/pg13
    export PGDATA=/data/pg13/pgdata
    export LD_LIBRARY_PATH=${PGHOME}/pgsql/lib:${LD_LIBRARY_PATH}
    export PATH=${PGHOME}/pgsql/bin:${PATH}
    postgres@developer:~$ . .bashrc

7. 初始化數據庫集簇

postgres@developer:~$ initdb -D $PGDATA -k 
    The files belonging to this database system will be owned by user "postgres".
    This user must also own the server process.

    The database cluster will be initialized with locale "en_US.UTF-8".
    The default database encoding has accordingly been set to "UTF8".
    The default text search configuration will be set to "english".

    Data page checksums are enabled.

    fixing permissions on existing directory /data/pg13/pgdata ... ok
    creating subdirectories ... ok
    selecting dynamic shared memory implementation ... posix
    selecting default max_connections ... 100
    selecting default shared_buffers ... 128MB
    selecting default time zone ... Etc/UTC
    creating configuration files ... ok
    running bootstrap script ... ok
    performing post-bootstrap initialization ... ok
    syncing data to disk ... ok

    initdb: warning: enabling "trust" authentication for local connections
    You can change this by editing pg_hba.conf or using the option -A, or
    --auth-local and --auth-host, the next time you run initdb.

    Success. You can now start the database server using:

        pg_ctl -D /data/pg13/pgdata -l logfile start

8. 啟動數據庫

postgres@developer:~$ pg_ctl start -D $PGDATA -l /tmp/logfile
waiting for server to start.... done
server started

9. 登錄數據庫

postgres@developer:~$ psql -d postgres
psql (13.5)
Type "help" for help.


postgres=# select version();
                                               version                                                
------------------------------------------------------------------------------------------------------
PostgreSQL 13.5 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit
(1 row)

總結
上面兩種方式為 PostgreSQL 在 Ubuntu 操作系統下的安裝。需要注意的是 Ubuntu 系統中,使用 apt-get 安裝的 PostgreSQL 是基於發行 PostgreSQL 的二次封裝軟件。因此命令和源碼編譯安裝的會有所區別。如集簇的啟動,數據庫參數的修改,數據庫的刪除和創建,用戶的創建和刪除等等。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM