PostgreSQL數據庫的安裝與配置


項目中要用PostgreSQL,所以專門學習了一下如何安裝和調試postgresql,此博文用來記錄自己的安裝和調試過程,安裝環境是centos7。

首先嘗試了yum安裝,因為畢竟yum安裝簡單粗暴,官網的安裝指導地址:

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

安裝了半天,報錯,以為是自己的yum源有問題,換了幾個,還是不行,所以只能放棄這種安裝方式。

接下來嘗試源碼編譯安裝,我們首先從官網取Source,官網在這里:

https://www.postgresql.org/ftp/source/

打開如下:

目前已經最新的版本已經到10了,但最新的穩定版是9.6,所以我下載就是9.6這個版本,執行下面命令下載:

wget https://www.postgresql.org/ftp/source/v9.6.2/

取到source后,准備開始編譯安裝。

這里需要注意的postgresql編譯需要預裝一些工具。除了make、gcc(GNU編譯器套件)這些基本必備的工具,還要有zlib、bison等等,也不用提前裝,反正安裝過程中缺少什么再裝就好了。

取到源碼,先解壓:

tar -zxvf postgresql-9.6.2.tar.gz

進入postgresql-9.6.2文件夾:

再執行下面指令:

export CFLAGS = "-g -Wall -Wmissing-prototypes -Wpointer-arith 
-Wdeclaration-after-statement -Wendif-labels -Wformat-security 
-fno-strict-aliasing -fwrapv"

然后再執行下列指令

./configure --prefix=/opt/psql --with-perl --with-tcl --with-python --with-openssl 
--with-pam --without-ldap --with-libxml --with-libxslt --enable-thread-safety 
--with-wal-blocksize=16 --with-blocksize=16 --enable-dtrace --enable-debug  

其中--prefix是指定軟件的安裝路徑,--with選項是指安裝本文件依賴的庫文件。如有不清楚可以自己學習下configure命令的相關參數。

運行之后,會出現報錯,我這個報的錯還挺多,我都貼上來,供大家參考一下:

問題1:  
checking for dtrace... no  
configure: error: dtrace not found  
解決方法:  
yum search dtrace  
Loaded plugins: fastestmirror, refresh-packagekit, security  
Loading mirror speeds from cached hostfile  
 * base: mirrors.163.com  
 * extras: mirrors.163.com  
 * updates: mirrors.163.com  
=============================================================================================== Matched: dtrace ===============================================================================================  
systemtap-sdt-devel.i686 : Static probe support tools  
systemtap-sdt-devel.x86_64 : Static probe support tools  
  
找到了,就安裝,我是64位的,安裝第二個  
[root@localhost postgresql-9.3.5]# yum install -y systemtap-sdt-devel.x86_64  

問題2:  
checking for flags to link embedded Perl... Can't locate ExtUtils/Embed.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).  
BEGIN failed--compilation aborted.  
no  
configure: error: could not determine flags for linking embedded Perl.  
This probably means that ExtUtils::Embed or ExtUtils::MakeMaker is not  
installed.  
解決方法:  
yum install perl-ExtUtils-Embed -y  

問題3:  
configure: error: could not determine flags for linking embedded Perl.  
This probably means that ExtUtils::Embed or ExtUtils::MakeMaker is not  
installed.  
解決方法:  
yum install perl-ExtUtils-Embed  
  
問題4:  
configure: error: readline library not found  
If you have readline already installed, see config.log for details on the  
failure. It is possible the compiler isn't looking in the proper directory.  
Use --without-readline to disable readline support.  
  
解決方法:  
yum install readline readline-devel  
  
問題5:  
checking for inflate in -lz... no  
configure: error: zlib library not found  
If you have zlib already installed, see config.log for details on the  
failure. It is possible the compiler isn't looking in the proper directory.  
Use --without-zlib to disable zlib support.  
解決方法:  
yum install zlib zlib-devel  
  
  
問題6:  
checking for CRYPTO_new_ex_data in -lcrypto... no  
configure: error: library 'crypto' is required for OpenSSL  
解決方法:  
yum install openssl openssl-devel  
  
問題7:  
checking for pam_start in -lpam... no  
configure: error: library 'pam' is required for PAM  
解決方法:  
yum install pam pam-devel  
  
問題8:  
checking for xmlSaveToBuffer in -lxml2... no  
configure: error: library 'xml2' (version >= 2.6.23) is required for XML support  
解決方法:  
yum install libxml2 libxml2-devel  
  
問題9:  
checking for xsltCleanupGlobals in -lxslt... no  
configure: error: library 'xslt' is required for XSLT support  
解決方法:  
yum install libxslt libxslt-devel  
  
  
問題10:  
configure: error: Tcl shell not found  
解決方法:  
yum install tcl tcl-devel  
  
  
問題11:  
checking for ldap.h... no  
configure: error: header file is required for LDAP  
解決方法:  
yum install openldap openldap-devel  
  
問題12:  
checking for Python.h... no  
configure: error: header file <Python.h> is required for Python  
解決方法:  
yum install python python-devel 

以上這些依賴庫都安裝成功后,再運行上面的configure命令,就能成功啦。

接下來就是編譯安裝啦:

make && make install

這兩個命令可能會比較慢,執行完成后,會提示成功。

然后你就會發現在/opt/目錄下找到你安裝的psql了

其中:

/opt/psql/bin里面放的是可執行命令,比如createdb之類的;

/opt/psql/lib里面放的是庫文件;

/opt/psql/include里面放的是頭文件;

/opt/psql/share是相關的資源文件。

這些文件如果在configure命令中沒有指定--prefix的話,會安裝到/usr/local/目錄下,以后要刪除的時候就要一個個找了,比較麻煩。

好的 我們安裝好了postgresql程序,接下來進行配置。

為了安全考慮,postgresql不允許使用root用戶操作數據庫,我們在系統中為使用postgresql添加一個用戶postgres:

adduser postgres

創建密碼:

passwd postgres

然后切換到postgres用戶下面:

su - postgres

編輯/home/postgres下的.bash_profile

設置以下的環境變量

export PGHOME=/opt/psql  (這個就是我們的安裝目錄)

export PGDATA=~/data        (數據存放的目錄,這個看你高興了,不要求一定放在這里)

export PATH=$PATH:$HOME/bin:$PGHOME/bin

然后source一下

source  ~/.bash_profile

使環境變量生效。

接下來初始化數據庫,使用initdb命令(如果提示command not found,那么很有可能是你上面的PGHOME設置錯誤或者沒有source一下)

#初始化數據庫
initdb 

數據庫的初始化完成后運行:

pg_ctl start

啟動postgres數據庫實例。此時你就可以使用:

ps -ef | grep postgres

現在我們可以進入數據庫,使用如下命令:

psql -h 127.0.0.1 -d postgres -U postgres

  

如果我們比較懶,不想每次登錄手動啟動psql,那么設置下psql開機啟動。

PostgreSQL的開機自啟動腳本位於PostgreSQL源碼目錄的contrib/start-scripts路徑下:

  

linux文件即為自啟動腳本。

1)修改linux文件屬性,添加X屬性(這里如果提示無權限的話,切換到root用戶進行操作)

chmod a+x linux

2) 復制linux文件到/etc/init.d目錄下,更名為postgresql

cp linux /etc/init.d/postgresql

3)修改/etc/init.d/postgresql文件的兩個變量

prefix設置為postgresql的安裝路徑:/opt/psql

PGDATA設置為postgresql的數據目錄路徑:/home/postgres/data

執行service postgresql start,就可以啟動PostgreSQL服務

4) 執行service postgresql start,就可以啟動PostgreSQL服務

service postgresql start

5)設置postgresql服務開機自啟動

chkconfig --add postgresql

執行上面的命令,就可以實現postgresql服務的開機自啟動。

接下來就是PostgreSQL的遠程連接設置

1) 設置遠程訪問認證機制

編輯/home/postgres/PGHOME/pg_dat/pg_hba.conf 文件:

說明:

每一行有五個字段,

# TYPE DATABASE USER CIDR-ADDRESS METHOD

分別是:連接類型、可使用的數據庫名、使用者、DIDR地址、和驗證方法等五項。

在該配置文件中的加上圖中最后兩行 IPv4 remote connections的配置,把該網段的IP配置進去,就可以。

2. 改監聽地址

默認下,POSTGRESQL只接受本地服務,要接受遠程服務,需改postgresql.conf 文件listen_address = *

配置完后,重新啟動一下數據庫,既可以進行遠程訪問數據庫了。

以上是我安裝配置PostgreSQL的大概過程,后續遇到的問題會在文章中持續更新,有錯誤歡迎提出指正,謝謝~

 


免責聲明!

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



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