1.準備工作
環境:CentOS 6.8
關閉防火牆
$: service iptables stop # 重啓後失效
$: chkconfig iptables off # 禁用防火牆
在官方網站下載對應版本tar包,本文使用postgresql 8.2.15版本:postgresql-8.2.15.tar.gz 官方下載網址:https://www.postgresql.org/ftp/source/v8.2.15/
2.安裝依賴
$: yum -y install gcc
$: yum -y install gcc-c++
$: yum -y install readline-devel
$: yum -y install zlib-devel
3.開始安裝
3.1 解壓安裝包 安裝包放在/usr/local下
$: cd /usr/local
$: tar -zvxf postgresql-8.2.15.tar.gz
進入postgresql-8.2.15
$: cd postgresql-8.2.15
3.2 創建Linux “postgres”用戶
$: adduser postgres
$: passwd postgres # 創建用戶密碼
3.3 在/usr/local/postgresql-8.2.15 目錄下開始安裝
$: cd /usr/local/postgresql-8.2.15
// 安裝依賴
$: yum -y install make
// 配置
$: ./configure --prefix=/usr/local/postgresql
// 編譯
$: make
// 安裝
$: make install
3.4 配置環境變量
$: vi /etc/profile
// 在/etc/profile文件的最後一行添加如下內容:
PATH=$PATH:/usr/local/postgresql/bin
更新環境變量:
$: source /etc/profile
3.5 初始化數據庫
在/usr/local/postgresql目錄下初始化數據庫:
$: cd /usr/local/postgresql
$: mkdir data
$: chown postgres:postgres /usr/local/postgresql/data/
$: su postgres
$: /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data/
3.6 複製並修改配置文件(修改存放數據目錄)
切換到root用戶下進行復制及修改配置文件操作
$: su root
$: “Password” # 輸入root密碼
//複製安裝目錄下的linux文件到/etc/init.d/中,並將linux名稱重命名爲postgresql
$: cd /usr/local/postgresql-8.2.15
$: cp /usr/local/postgresql-8.2.15/contrib/start-scripts/linux /etc/init.d/postgresql
//編輯複製出來的文件
$: vi /etc/init.d/postgresql
//修改以下內容即可
# Installation prefix
prefix=/usr/local/postgresql
# Data directory
PGDATA="/usr/local/postgresql/data"
$: chmod +x /etc/init.d/postgresql
3.7 啓動數據庫和設置開機自啓
$: /etc/init.d/postgresql start
$: chkconfig postgresql on
3.8 創建數據庫操作的歷史文件
$: touch /usr/local/postgresql/.pgsql_history
$: chown postgres:postgres /usr/local/postgresql/.pgsql_history
3.9 測試數據庫是否創建成功,並且連接數據庫
$: cd /usr/local/postgresql-8.2.15
$: su postgres
$: createdb test
$: psql test
// \q可以退出
Welcome to psql 8.2.15, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
test=# \q
4.修改數據庫外網訪問
4.1 修改數據庫外網訪問之前先關閉數據庫
$: su root
$: “Password” # 輸入root密碼
$: /etc/init.d/postgresql stop
4.2 修改pg_hba.conf
$: vi /usr/local/postgresql/data/pg_hba.conf
// 找到IPv4 local connections
# IPv4 local connections:
host all all 127.0.0.1/32 trust
// 修改如下藍色字體參數
# IPv4 local connections:
host all all 192.168.1.0/24 md5 #根據實際網段填寫
4.3 修改postgresql.conf
$: vi /usr/local/postgresql/data/postgresql.conf
// 找到listen_addresses:
# listen_addresses = 'localhost'
// 刪除#號,更改如下
listen_addresses = '*'
4.4 再次啓動postgresql
$: /etc/init.d/postgresql start
$: su postgres # 切換用戶
$: psql -U postgres # 進入交互式
// 修改密碼,本次密碼設置爲1234
alter user postgres with password '1234';
# \q 可以退出
4.5 安裝完成,我們也可輸入主機IP,通過以下命令方式進入postgresql
$: psql -h 192.168.1.XXX -d postgres -U postgres -p 5432
$: “Password” # 輸入剛更改的數據庫用戶postgres密碼1234