镜像方式安装:
1.下载依赖以及脚本
[email protected]:a356a/deploy.git
2.执行脚本-postgresImage.sh
脚本具体执行:
关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
拉取镜像并启动镜像
docker pull postgres:12-alpine3.16
docker run --name postgresDocker12 -e POSTGRES_PASSWORD=88888888 -p 5432:5432 -d postgres:12-alpine3.16
修改pg_hba.conf便于远程访问[改完以后要重启]
CONTAINER_ID=$(docker ps |grep postgresDocker12|awk '{print $1}')
docker cp ${CONTAINER_ID}:/var/lib/postgresql/data/pg_hba.conf .
sed -i s?'host all all all md5'?'host all all all password'?g pg_hba.conf
echo "host all root all password" >> pg_hba.conf
docker cp pg_hba.conf ${CONTAINER_ID}:/var/lib/postgresql/data/
docker restart ${CONTAINER_ID}
通过SQL创建新用户 便于远程连接测试
echo "CREATE USER root WITH PASSWORD '88888888' superuser;" > aaa.sql
echo "GRANT ALL ON * TO root;" >> aaa.sql
echo "create database test;" >> aaa.sql
docker cp aaa.sql ${CONTAINER_ID}:/root
docker exec -d ${CONTAINER_ID} psql 'host=localhost port=5432 user=postgres dbname=postgres' -f /root/aaa.sql
ps:必须要检查 云服务器 的安全组策略,出方向、入方向都要开放postgres的端口[5432],否则很容易无法远程连上
ps:防火墙必须开放postgres的端口[5432],不然,也无法远程连上,学习使用的话,关掉也可以