docker一鍵搭建postgresql腳本(全網精華版原創)

前言

本篇文章將演示如何利用docker快速搭建postgresql數據庫,並提供了一鍵安裝腳本用於快速安裝。html

腳本

腳本列表

start_docker_postgres.sh:一鍵安裝腳本sql

pg_hba.conf:postgresql的訪問限制配置文件docker

postgresql.conf:postgresql的主配置文件數據庫

腳本內容

pg_hba.conf微信

# "local" is for Unix domain socket connections only運維

local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
host all all 0.0.0.1/0 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trustdom

host all all all md5socket

postgresql.confide

listen_addresses = '*'post

max_wal_size = 1GB
min_wal_size = 80MB

log_timezone = 'Etc/UTC'


datestyle = 'iso, mdy'
timezone = 'Etc/UTC'


default_text_search_config = 'pg_catalog.english'

start_docker_postgres.sh

PG_PASSWD=postgres
PG_HOME=/home/postgresql/data
PG_VERSION=$1

if [ "$PG_VERSION" == "" ];then
PG_VERSION=10.0
fi

echo "start to run docker for postgres:$PG_VERSION"

docker rm -f postgres
docker volume prune -f
sleep 3

if [ -d "$PG_HOME" ];then
read -r -p "$PG_HOME exists.Are You Sure? [Y/n] " input

case $input in
[yY][eE][sS]|[yY])
echo "rm -rf $PG_HOME"
rm -rf $PG_HOME
mkdir -p $PG_HOME
;;

[nN][oO]|[nN])
echo "Exit Setup for pg..."
exit 0
;;

*)
echo "Invalid input..."
exit 1
;;
esac
else
echo "mkdir -p $PG_HOME"
mkdir -p $PG_HOME
fi

cmd="docker run -it --name postgres --restart always -e POSTGRES_PASSWORD="$PG_PASSWD" -e ALLOW_IP_RANGE=0.0.0.0/0 -v $PG_HOME:/var/lib/postgresql -p 55433:5432 -d postgres:$PG_VERSION"
echo $cmd
eval $cmd

#wait to startup
sleep 10
#/home/postgres/data:/var/lib/postgresql

docker cp pg_hba.conf postgres:/var/lib/postgresql/data/pg_hba.conf
docker cp postgresql.conf postgres:/var/lib/postgresql/data/postgresql.conf

docker restart postgres

運行

環境依賴

Linux下須要預安裝好docker環境:(參考:如何安裝docker

運行方式

sh start_docker_postgres.sh

以下圖,若是非首次安裝,須要刪除原有的數據目錄,輸入Y;首次安裝通常沒有該提示。


測試登錄

輸入:psql -h127.0.0.1 -p55433 -U postgres

以上就是我寫得一個快速搭建單機版postgresql的腳本,但願對你有幫助,有問題請在後面留言~

博主:測試生財(一個不爲996而996的測開碼農)

座右銘:專一測試開發與自動化運維,努力讀書思考寫做,爲內卷的人生奠基財務自由。

內容範疇:技術提高,職場雜談,事業發展,閱讀寫做,投資理財,健康人生。

csdn:https://blog.csdn.net/ccgshigao

博客園:https://www.cnblogs.com/qa-freeroad/

51cto:https://blog.51cto.com/14900374

微信公衆號:測試生財(按期分享獨家內容和資源)

相關文章
相關標籤/搜索