Postgresql 常用命令及腳本

#!/usr/bin/env bash
#################################################
#
#   Author    Ryan
#   Email     [email protected]
#   Date      2020年6月22日
#
###############################################################


yum install -y gcc gcc-c++
yum -y install sysstat iotop tree lrzsz rsync net-tools


ZLIB_VERSION="1.2.11"
OPENSSL_VERSION="openssl-1.0.2r"
POSTGRESQL_VERSION="10.11-1"

# 登錄 pg shell
bin/pgsql -U postgres -p 5432

useradd -m postgres

# 把用戶 adisp 追加到 postgres 組中
usermod -a -G postgres adisp

# 設置密碼
passwd postgres


if [ ! -f "./postgresql-${OPENSSL_VERSION}.tar.gz" ];then
    wget http://get.enterprisedb.com/postgresql/postgresql-${POSTGRESQL_VERSION}-linux-x64-binaries.tar.gz
fi

if [ ! -f "openssl-${OPENSSL_VERSION}.tar.gz" ];then
    echo "Download File () Fail !!!"
    exit 1    
fi

tar -zxvf postgresql-${POSTGRESQL_VERSION}-linux-x64-binaries.tar.gz


cd pgsql

./configure --build=Ryan --prefix=/opt/software/postgresql-10.10.2 --exec-prefix=/opt/software/postgresql-10.10.2/bin


bin/initdb -D /home/postgres/pgsql/data


# 設置 連接數      data/postgresql.conf
# 設置訪問地址   data/pg.hba.conf  爲0.0.0.0/0


bin/pg_ctl -D /home/postgres/pgsql/data -l logfile start
bin/pg_ctl -D /home/postgres/pgsql/data stop

sed -i "s/\/opt\/software/\/home\/adisp\/g" ./*.sh &
sed -i "s/\/opt\/software/\/home\/adisp\/g" ./*.properties


sed -i "s/\/opt\/software/\/home\/adisp\/g" ./*.sh &
sed -i "s/\/opt\/software/\/home\/adisp\/g" ./*.properties


make && make install

echo 'Make postgres Success !!!'


==========================================================================
==========================================================================
                            源碼編譯安裝 postgresql
==========================================================================
==========================================================================

./configure --prefix=/opt/software/postgresql-12.5-bin --enable-profiling --enable-dtrace --enable-cassert --with-tcl --with-perl --with-python --with-gssapi --with-openssl --with-libxml


make && make install


# 新增 postgres 用戶
useradd postgres
passwd postgres

# 切換用戶
su - postgres

mkdir /opt/software/postgres-12.5-bin/data

bin/initdb -D /opt/software/postgres-12.5-bin/data

# 啓動數據庫

PG_HOME=/opt/software/postgresql-12.5-bin
PG_DATA_HOME=${PG_HOME}/data

bin/pg_ctl -D $PG_DATA_HOME start
bin/pg_ctl -D $PG_DATA_HOME -l logfile start
bin/pg_ctl -D $PG_DATA_HOME --log=./logs/server.log start
bin/pg_ctl -D $PG_DATA_HOME restart

pg_ctl logrotate -D $PG_DATA_HOME -w


# 關閉數據庫
bin/pg_ctl stop -D $PG_DATA_HOME

bin/pg_ctl stop -D $PG_DATA_HOME -m [smart|fast|immediate] -w -s 


# 登錄數據庫
bin/psql

# 創建用戶
create user sonar with password 'sonar';
# 賦權限
ALTER user sonar with createdb;

# 修改用戶密碼
ALTER USER postgres WITH PASSWORD 'postgres';
ALTER user sonar with password 'sonar';

# 創建一個數據庫,數據庫擁有者sonar
create database sonar owner sonar;

# 給用戶授權
GRANT ALL PRIVILEGES ON DATABASE sonar TO sonar;


# 開啓遠程訪問,修改:
vim /data/postgresql.conf

listening_address: '*'


vim data/pg_hba.conf
# 添加/修改:允許任意用戶從任意機器上以密碼方式訪問數據庫,把下行添加爲第一條規則:
host    all             all             0.0.0.0/0               md5


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章