postgres安装记录-centos

镜像方式安装:

1.下载依赖以及脚本

git@github.com: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
echo "host all root 0.0.0.0/0 trust" >> 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 "alter user root with password '88888888';" >> 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
docker exec -d ${CONTAINER_ID} psql --host=localhost --port=5432 --username=postgres --dbname=postgres -f /root/aaa.sql

ps:必须要检查 云服务器 的安全组策略,出方向、入方向都要开放postgres的端口[5432],否则很容易无法远程连上
ps:防火墙必须开放postgres的端口[5432],不然,也无法远程连上,学习使用的话,关掉也可以

posted @ 2023-07-02 07:07  356a  阅读(10)  评论(0编辑  收藏  举报