[英]unable to run script to create multiple database in docker
I am trying to create multiple databases in one container. 我试图在一个容器中创建多个数据库。 I am following the link 我正在追踪连结
With some reason, I don't see initdb shell script running for me 由于某种原因,我看不到initdb shell脚本为我运行
docker-compose looks like 码头工人组成看起来像
postgres:
image: postgres:alpine
container_name: postgres
environment:
POSTGRES_DB: employee
POSTGRES_MULTIPLE_DATABASES: employee_test
POSTGRES_USER: test
POSTGRES_PASSWORD: test
volumes:
- "./initdb.sh:/docker-entrypoint-initdb.d/initdb.sh"
- "./.docker_pgdata:/var/lib/postgresql/data"
ports:
- "5432:5432"
I started by POSTGRES_MULTIPLE_DATABASES: employee,employee_test but I got an error that employee database is already created. 我从POSTGRES_MULTIPLE_DATABASES开始:employee,employee_test,但出现一个错误,指出已经创建了employee数据库。
shell script 外壳脚本
function create_user_and_database() {
local database=$1
echo " Creating user and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$database"<<-EOSQL
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $POSTGRES_USER;
EOSQL
}
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
PGPASSWORD=$POSTGRES_PASSWORD
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
create_user_and_database $db
done
echo "Multiple databases created"
fi
When I run the script directly it complains about 当我直接运行脚本时,它抱怨
Creating user and database 'employee_test'
psql: FATAL: database "employee_test" does not exist
Not sure, id the create database command run or not. 不确定id id create database命令是否运行。 Can someone help what can be the issue? 有人可以帮忙解决什么问题吗?
When you mount backup volume it will not run the init
script of DB creation. 当您装载备份卷时,它将不运行数据库创建的init
脚本。
So you have two option 所以你有两个选择
init
script. 删除备份卷并运行init
脚本。 If you check the entrypoint or docker logs you will see 如果您检查入口点或docker日志,您将看到
Recreating postgres ...
WARNING: Service "postgres" is using volume "/var/lib/postgresql/data" from the previous container. Host mapping.....
So the init script will ignore during startup due to the backup volume 因此,由于备份量大,init脚本在启动期间将被忽略
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.