介紹
本文主要介紹linux系統上安裝postgreSql數據庫的一般思路,並提供了一個可執行腳本,在下載pg安裝包后,修改腳本的部分配置,執行腳本,就可以完成數據庫的安裝、配置和啟動,腳本在文末提供。
下載安裝包
腳本的執行依賴現有安裝包,不支持使用命令獲取pg安裝包,可以手動下載后,與腳本放置到統一目錄下
解壓安裝包
這個步驟是把安裝包解壓到指定目錄下
# unzip pg function pg_untar(){ if [ -d ${pg_deploy_path} ];then echo "pg has been installed" else if [ ! -e ${pg_name} ];then echo "Missing pg installation package" else mkdir -p ${pg_deploy_path} tar -zxf ${pg_name} -C ${pg_deploy_path} fi fi }
新增postgres用戶
為數據庫創建默認的postgres用戶,用於登錄並操作數據庫
備注:對用戶的密碼進行了加密,具體介紹參見:Linux下利用glibc2庫和crypt()函數生成用戶密碼
# Create a Linux user to log in to the PG database function pg_create_user(){ # user_exist=`cat /etc/passwd | grep '^${user_name}:' -c` egrep "^${user_name}" /etc/passwd >/dev/null if [ $? -eq 1 ]; then pass=$(perl -e 'print crypt($ARGV[0], "password")' $user_name) useradd -m -p $pass ${user_name} #useradd -m -p 'postgres' ${user_name} [ $? -eq 0 ] && echo "user [${user_name}] has been added to system!" || echo "Failed to add user [${user_name}]" else echo "user [${user_name}] exists" fi }
初始化數據庫
創建用戶后,需要對數據庫進行初始化,主要是初始化數據庫的數據存儲目錄。
備注:
1. 把數據庫的數據存儲目錄擁有者改為postgres用戶
2. 腳本的執行需要root權限,所以對數據庫的初始化,需要切換到postgres用戶。
#init pg function pg_init(){ #Create a data directory mkdir -p ${pg_data_path} touch ${pg_deploy_path}/logfile #Grant user access chown -R ${user_name} ${pg_data_path} chown ${user_name} ${pg_deploy_path}/logfile # init database su - ${user_name} -c "${pg_deploy_path}/pgsql/bin/initdb -D ${pg_data_path}" }
更改配置文件
1. 修改數據庫的綁定ip,默認綁定的是localhost,不能使用部署服務器ip訪問
2. 修改對客戶端ip的限制,允許任意客戶端連接
#Modify config of the pg database function pg_modify_config(){ id_path=${pg_data_path}/postgresql.conf local_path=${pg_data_path}/pg_hba.conf localip=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:") if [ -d ${pg_data_path} ];then chown -R 'root' ${pg_data_path} #Modify listening ip sed -i "s@#listen_addresses = 'localhost'@listen_addresses = \'${localip}\'@g" $id_path #Modify Trust client ip #sed -i "s@#listen_addresses = 'localhost'@listen_addresses = \'${localip}\'@g" $id_path sed '86 ahost all all 0.0.0.0/0 trust' -i $local_path chown -R ${user_name} ${pg_data_path} else echo "You need to initialize the database with the command:\n\t ${pg_deploy_path}/pgsql/bin/initdb -D ${pg_data_path} " fi }
啟動數據庫
數據庫的操作都需要切換到postgres用戶
# start pg database function pg_start(){ su - ${user_name} -c "${pg_deploy_path}/pgsql/bin/pg_ctl -D ${pg_data_path} -l ${pg_deploy_path}/logfile start" }
開放接口
一般數據庫部署后,本機能調試操作,其他服務器無法連接,需要開放端口
#Open 5432 port function pg_open_port(){ isopen=$(firewall-cmd --query-port=5432/tcp) if [ 'no' == $isopen ];then firewall-cmd --add-port=5432/tcp --permanent>/dev/null firewall-cmd --reload>/dev/null else echo "port 5432 already opened" fi }
以上是所有的安裝步驟解釋,下面是完整腳本:

#!/bin/bash # install postgre # Pg database installation package name pg_name='postgresql-9.5.16-1-linux-x64-binaries.tar.gz' # Pg database installation path pg_deploy_path='/opt/ops/pg' # Directory of data stored in the pg database pg_data_path="${pg_deploy_path}/data" # Directory of data stored in the pg database user_name='postgres' # unzip pg function pg_untar(){ if [ -d ${pg_deploy_path} ];then echo "pg has been installed" else if [ ! -e ${pg_name} ];then echo "Missing pg installation package" else mkdir -p ${pg_deploy_path} tar -zxf ${pg_name} -C ${pg_deploy_path} fi fi } # Create a Linux user to log in to the PG database function pg_create_user(){ # user_exist=`cat /etc/passwd | grep '^${user_name}:' -c` egrep "^${user_name}" /etc/passwd >/dev/null if [ $? -eq 1 ]; then pass=$(perl -e 'print crypt($ARGV[0], "password")' $user_name) useradd -m -p $pass ${user_name} #useradd -m -p 'postgres' ${user_name} [ $? -eq 0 ] && echo "user [${user_name}] has been added to system!" || echo "Failed to add user [${user_name}]" else echo "user [${user_name}] exists" fi } #init pg function pg_init(){ #Create a data directory mkdir -p ${pg_data_path} touch ${pg_deploy_path}/logfile #Grant user access chown -R ${user_name} ${pg_data_path} chown ${user_name} ${pg_deploy_path}/logfile # init database su - ${user_name} -c "${pg_deploy_path}/pgsql/bin/initdb -D ${pg_data_path}" } #Modify config of the pg database function pg_modify_config(){ id_path=${pg_data_path}/postgresql.conf local_path=${pg_data_path}/pg_hba.conf localip=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:") if [ -d ${pg_data_path} ];then chown -R 'root' ${pg_data_path} #Modify listening ip sed -i "s@#listen_addresses = 'localhost'@listen_addresses = \'${localip}\'@g" $id_path #Modify Trust client ip #sed -i "s@#listen_addresses = 'localhost'@listen_addresses = \'${localip}\'@g" $id_path sed '86 ahost all all 0.0.0.0/0 trust' -i $local_path chown -R ${user_name} ${pg_data_path} else echo "You need to initialize the database with the command:\n\t ${pg_deploy_path}/pgsql/bin/initdb -D ${pg_data_path} " fi } # start pg database function pg_start(){ su - ${user_name} -c "${pg_deploy_path}/pgsql/bin/pg_ctl -D ${pg_data_path} -l ${pg_deploy_path}/logfile start" } #Open 5432 port function pg_open_port(){ isopen=$(firewall-cmd --query-port=5432/tcp) if [ 'no' == $isopen ];then firewall-cmd --add-port=5432/tcp --permanent>/dev/null firewall-cmd --reload>/dev/null else echo "port 5432 already opened" fi } echo "============== start install pg ==============" if [ -d ${pg_deploy_path} ];then echo "pg has been installed" else pg_untar pg_create_user pg_init pg_modify_config pg_start pg_open_port fi echo "============== finish installing pg =============="