Postgresql源碼安裝


 

以在64位CentOS6.5操作系統上源碼安裝postgresql-9.6beta1為例

 

一.進入官網下載代碼(postgresql-9.6beta1.tar.gz)

https://www.postgresql.org

 

二.將源碼上傳到服務器/home/alian目錄下

可以在windows安裝ssh或xftp工具,也可以在CentOS安裝lrzsz的rpm包(使用rz,sz命令)上傳或下載文件。

 

三.將源碼在當前目錄下解壓

[root@localhost alian]# tar xzvf postgresql-9.6beta1.tar.gz

 

四.進入解壓后的目錄postgresql-9.6beta1,使用configure工具查看編譯幫助

[root@localhost postgresql-9.6beta1]# ./configure --help

 

五.根據幫助中各配置描述選擇適合自己的配置選項

如下命令中

--prefix=/opt/pg9.6

pg軟件的安裝路徑

 

以下4個配置不是必須的,但如果想修改必須要重新initdb

--with-segsize=1

表在操作系統上物理文件大小,單位GB,默認值為1,如果表大於1GB,將在操作系統上分割成多個1GB的文件。

--with-blocksize=32

塊大小,單位KB,默認值為8,是表數據I/O和存儲的單元,范圍1-32,且必須是2的N次方。

--with-wal-segsize=32

WAL在操作系統上物理文件大小,單位MB,默認值16,范圍1-64,且必須是2的N次方。

--with-wal-blocksize=64

WAL塊大小,單位KB,是WAL I/O和存儲的單元,范圍1-64,且必須是2的N次方。

[root@localhost postgresql-9.6beta1]# ./configure --prefix=/opt/pg9.6 --with-segsize=1 --with-blocksize=32 --with-wal-segsize=32 --with-wal-blocksize=64


checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether to build with 64-bit integer date/time support... yes
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 32kB
checking for segment size... 1GB
checking for WAL block size... 64kB
checking for WAL segment size... 32MB
checking for gcc... gcc

..............

 

 

configure過程中可能會出現下面的錯誤,原因是缺少對應的庫,安裝對應的庫即可。

configure: error: readline library not found

[root@localhost postgresql-9.6beta1]# yum install readline-devel

 configure: error: zlib library not found

[root@localhost postgresql-9.6beta1]# yum install zlib-devel

 

 

六.configure成功后,可以直接執行make命令進行簡單編譯,也可以執行make world將contrib目錄下的擴展工具一起編譯,當然也可以先進行簡單編譯,然后進入contrib下對某一工具進行單獨編譯

方法一:簡單安裝

1.執行命令

[root@localhost postgresql-9.6beta1]# make && make install

命令執行完之后,pg軟件就被安裝到/opt/pg9.6路徑下

[root@localhost postgresql-9.6beta1]# ls /opt/pg9.6/
bin include lib share

 

2.如果想安裝contrib下的某一擴展工具,以檢測數據庫運行的pg_stat_statements為例

進入contrib/pg_stat_statements目錄下(前提是已在主目錄下執行了configure)

[root@localhost postgresql-9.6beta1]# cd contrib/pg_stat_statements/

 

執行命令

[root@localhost pg_stat_statements]# make && make install

 

方法二、全部安裝(contrib下所有的擴展工具)

[root@localhost postgresql-9.6beta1]# make world && make install-world

 

七、軟件安裝完成后,進行初始化

1.新建一個用戶postgres

[root@localhost postgresql-9.6beta1]# groupadd postgres

[root@localhost postgresql-9.6beta1]# useradd postgres

 

2.創建PGDATA目錄

[root@localhost postgresql-9.6beta1]# mkdir -p /mnt/pgdata
[root@localhost postgresql-9.6beta1]# chown postgres:postgres /mnt/pgdata/

 

3.使用postgres用戶初始化

[root@localhost postgresql-9.6beta1]# su postgres

[postgres@localhost postgresql-9.6beta1]$ /opt/pg9.6/bin/initdb -D /mnt/pgdata -E UTF8 --locale=C

 

4.使用postgres用戶啟停數據庫

[postgres@localhost postgresql-9.6beta1]$ /opt/pg9.6/bin/pg_ctl -D /mnt/pgdata/ start
server starting

[postgres@localhost postgresql-9.6beta1]$ /opt/pg9.6/bin/pg_ctl -D /mnt/pgdata/ stop
waiting for server to shut down....LOG: database system is shut down
done
server stopped

 

5.修改配置文件,使用客戶端訪問數據庫

初始化完成后,會自動為數據庫創建一個超級用戶postgres(執行initdb的用戶),但是數據庫還沒有給該用戶設置訪問密碼,所以啟動數據庫后,執行下面的命令,給該用戶配置一個密碼

[postgres@localhost postgresql-9.6beta1]$ /opt/pg9.6/bin/psql -U postgres
psql (9.6beta1)
Type "help" for help.

postgres=# alter user postgres with password 'password';
ALTER ROLE
postgres=# \q

 

另外,在PGDATA目錄下生成配置文件pg_hba.conf和postgresql.conf,

postgresql.conf中配置#listen_addresses = 'localhost',表示只監聽本地連接,將此配置改成*用來監聽所有IP的訪問,也可以指定特定的IP(注意前面的注釋符#刪掉)

listen_addresses = '*'

 

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 trust
# IPv6 local connections:
host all all ::1/128 trust

 

其中trust表示允許無密碼訪問,這是不安全的,將其改為md5,使用密碼訪問。

# "local" is for Unix domain socket connections only

local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5

 

修改完密碼和配置文件后重啟服務再次訪問數據庫時就會提示輸入密碼

[postgres@localhost postgresql-9.6beta1]$ /opt/pg9.6/bin/psql -U postgres
Password for user postgres: 
psql (9.6beta1)
Type "help" for help.

postgres=#

 

 


免責聲明!

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



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