一、安裝環境
-
postgresql-12.0
-
CentOS-7.6
-
注意:確認linux系統可以正常連接網絡,因為在后面需要添加依賴包。
-
二、pg數據庫安裝包下載
選擇要安裝的版本進行下載:
-
-
三、安裝依賴包
在要安裝postgresql數據庫的Linux服務器上執行以下命令安裝所需要的依賴包:
-
yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
四、安裝postgres
1、在根目錄下新建/opt/pgsql文件夾,并將pgsql的壓縮包移入。
-
-
2、解壓壓縮包
-
tar -zxvf postgresql-12.0.tar.gz
3、進入解壓后的文件夾
-
cd postgresql-12.0
4、編譯postgresql源碼
-
./configure --prefix=/opt/pgsql/postgresqlmakemake install
至此,已完成postgreql的安裝。進入/opt/pgsql/postgresql目錄可以看到安裝后的postgresql的文件。
-
五、創建用戶組postgres并創建用戶postgres
-
groupadd postgresuseradd -g postgres postgresid postgres
-
六、創建postgresql數據庫的數據主目錄并修改文件所有者
-
這個數據庫主目錄是隨實際情況而不同,這里我們的主目錄是在/opt/pgsql/postgresql/data目錄下
-
mkdir datachown postgres:postgres data
-
七、配置環境變量
進入home/postgres目錄可以看到.bash_profile文件。
-
cd /home/postgresls -al
編輯修改.bash_profile文件。
-
vim .bash_profile
添加以下內容。
-
export PGHOME=/opt/pgsql/postgresqlexport PGDATA=/opt/pgsql/postgresql/dataPATH=$PATH:$HOME/bin:$PGHOME/bin
保存,退出vim。執行以下命令,使環境變量生效
-
source .bash_profile
八、切換用戶到postgres并使用initdb初使用化數據庫
-
su - postgres
initdb
可以看到/opt/pgsql/postgresql/data已經有文件了。
cd /opt/pgsql/postgresql/data
九、配置服務
修改/opt/pgsql/postgresql/data目錄下的兩個文件。
postgresql.conf 配置PostgreSQL數據庫服務器的相應的參數。
pg_hba.conf 配置對數據庫的訪問權限。
vim postgresql.conf
其中,參數“listen_addresses”表示監聽的IP地址,默認是在localhost處監聽,也就是127.0.0.1的ip地址上監聽,只接受來自本機localhost的連接請求,這會讓遠程的主機無法登陸這臺數據庫,如果想從其他的機器上登陸這臺數據庫,需要把監聽地址改為實際網絡的地址,一種簡單的方法是,將行開頭的#去掉,把這個地址改為*,表示在本地的所有地址上監聽。
vim pg_hba.confhost all all 0.0.0.0/0 trust #新增這一行
找到最下面這一行,這樣局域網的人才能訪問
十、設置PostgreSQL開機自啟動
PostgreSQL的開機自啟動腳本位于PostgreSQL源碼目錄的contrib/start-scripts路徑下。
linux文件即為linux系統上的啟動腳本
cd /opt/pgsql/postgresql-12.0/contrib/start-scripts
切換為root用戶,修改linux文件屬性,添加X屬性
su root
chmod a+x linux
復制linux文件到/etc/init.d目錄下,更名為postgresql
cp linux /etc/init.d/postgresql
修改/etc/init.d/postgresql文件的兩個變量
prefix設置為postgresql的安裝路徑:/pgsql/postgresql
PGDATA設置為postgresql的數據目錄路徑:/pgsql/postgresql/data
vim /etc/init.d/postgresql
設置postgresql服務開機自啟動
chkconfig --add postgresql
執行service postgresql start,啟動PostgreSQL服務
service postgresql start