Created
April 11, 2024 00:18
-
-
Save llccing/3124dd81e1ab08d9933a607373266a80 to your computer and use it in GitHub Desktop.
初始化一个 postgres 数据库,使用随机密码,并且将连接字符串打印到当前server的一个文件中
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 生成随机密码 | |
POSTGRES_PASSWORD=$(openssl rand -base64 12) | |
# 数据库名称 | |
DATABASE_NAME=server | |
# 创建 Docker 卷 | |
docker volume create postgres_data | |
# 启动 PostgreSQL 容器 | |
docker run --name my_postgres \ | |
-e POSTGRES_PASSWORD=$POSTGRES_PASSWORD \ | |
-e POSTGRES_DB=$DATABASE_NAME \ | |
-v postgres_data:/var/lib/postgresql/data \ | |
-p 5432:5432 -d postgres | |
# 输出连接字符串并保存到当前目录下的db_credentials.txt | |
echo "PostgreSQL has been started. Here is the connection string:" > db_credentials.txt | |
echo "postgresql://postgres:$POSTGRES_PASSWORD@<Your_Server_IP>:5432/$DATABASE_NAME" >> db_credentials.txt | |
echo "NOTE: Replace <Your_Server_IP> with your actual server IP address." >> db_credentials.txt | |
# 在终端显示密码文件已生成的消息 | |
echo "Database credentials have been saved to db_credentials.txt in the current directory." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment