docker一键搭建postgresql脚本(全网精华版原创)
前言
本篇文章将演示如何利用docker快速搭建postgresql数据库,并提供了一键安装脚本用于快速安装。
脚本
脚本列表
start_docker_postgres.sh:一键安装脚本
pg_hba.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 trusthost all all all md5
postgresql.conf
listen_addresses = '*'
max_wal_size = 1GB
min_wal_size = 80MBlog_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=$1if [ "$PG_VERSION" == "" ];then
PG_VERSION=10.0
fiecho "start to run docker for postgres:$PG_VERSION"
docker rm -f postgres
docker volume prune -f
sleep 3if [ -d "$PG_HOME" ];then
read -r -p "$PG_HOME exists.Are You Sure? [Y/n] " inputcase $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
ficmd="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/postgresqldocker cp pg_hba.conf postgres:/var/lib/postgresql/data/pg_hba.conf
docker cp postgresql.conf postgres:/var/lib/postgresql/data/postgresql.confdocker restart postgres
运行
环境依赖
Linux下需要预安装好docker环境:(参考:如何安装docker)
运行方式
sh start_docker_postgres.sh
测试登陆
以上就是我写得一个快速搭建单机版postgresql的脚本,希望对你有帮助,有问题请在后面留言~
博主:测试生财(一个不为996而996的测开码农)
座右铭:专注测试开发与自动化运维,努力读书思考写作,为内卷的人生奠定财务自由。
内容范畴:技术提升,职场杂谈,事业发展,阅读写作,投资理财,健康人生。
csdn:https://blog.csdn.net/ccgshigao
博客园:https://www.cnblogs.com/qa-freeroad/
51cto:https://blog.51cto.com/14900374
微信公众号:测试生财(定期分享独家内容和资源)