安裝PostgreSQL數據庫(Linux篇)


0.編譯環境

  • Linux: CentOS 5.5
  • gcc: 4.1.2

1. 安裝PostgreSQL

1) 解壓postgresql-9.1.7.tar.bz2

#tar jxvf postgresql-9.1.7.tar.bz2

 

2) 進入解壓后的postgresql-9.1.7目錄

#cd postgresql-9.1.7

3) 編譯postgresql源碼

#./configure --prefix=/opt/pgsql-9.1.7

 #make

#make install

至此,完成postgresql的安裝。進入/opt/pgsql-9.1.7目錄可以看到安裝后的postgresql的文件。

#ls /opt/pgsql-9.1.7

2.創建postgresql數據庫

1) 創建postgres用戶

#useradd postgres

修改postgres密碼

#passwd postgres

2) 設置postgres用戶的環境變量

切換到postgres用戶

#su - postgres

進入postgres的主目錄

#cd ~

編輯~/.bash_profile文件

#vi ~/.bash_profile

設置以下的環境變量

export PGHOME=/opt/pgsql-9.1.7

export PGDATA=~/data

保存,退出vi。執行以下命令,使環境變量生效

#source ~/.bash_profile

3) 初始化postgres數據庫

#initdb

至此,完成postgres數據庫的初始化。

4) 啟動postgres數據庫實例

#pg_ctl start

可以看到postgresql數據庫實例已經啟動,通過下面的命令可以查看系統中運行的postgres進程

#ps -ef | grep postgres

5) 連接postgresql數據庫

#psql -h 127.0.0.1 -d postgres -U postgres

6) 停止postgresql數據庫實例

#pg_ctl stop

#ps -ef |  grep postgres

可以看到已經沒有postgres進程

3. 設置PostgreSQL開機自啟動

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

linux文件即為linux系統上的啟動腳本

1)修改linux文件屬性,添加X屬性

#chmod a+x linux

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

#cp linux /etc/init.d/postgresql

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

prefix設置為postgresql的安裝路徑:/opt/pgsql-9.1.2

PGDATA設置為postgresql的數據目錄路徑:

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

#service postgresql start

 

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

#chkconfig --add postgresql

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

 

 


免責聲明!

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



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