Docker is a solution to create multiple databases when starting PostgreSQL

Time:2021-10-15

1 Preface

In the article《Docker starts PostgreSQL and recommends several connection tools》In, we introduce how to passDockerTo startPostgreSQL, but there is only one database. If you want to create multiple databases in the same databaseDockerWhat about the container?

2 two schemes

One option is toshell/sqlScript put/docker-entrypoint-initdb.d/Directory, so that the container can be created automatically when it is started; The other is throughshellThe script specifies creation, which is essentially the same. Only the first one is introduced here.

holdshellScript orsqlWhen the script is placed in the specified directory, it will be executed automatically. Both scripts can be used.

shellScript examples are as follows:


#!/bin/bash

set -e
set -u

function create_user_and_database() {
	local database=$1
	echo "  Creating user and database '$database'"
	psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
	    CREATE USER $database;
	    CREATE DATABASE $database;
	    GRANT ALL PRIVILEGES ON DATABASE $database TO $database;
EOSQL
}

if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
	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

sqlScript examples are as follows:


CREATE USER pkslowuser;

CREATE DATABASE logdata;
GRANT ALL PRIVILEGES ON DATABASE logdata TO pkslowuser;

CREATE DATABASE orderdata;
GRANT ALL PRIVILEGES ON DATABASE orderdata TO pkslowuser;

CREATE DATABASE userdata;
GRANT ALL PRIVILEGES ON DATABASE userdata TO pkslowuser;

3 packaging start

get readyDockerfile, putshell/sqlPut the script file into the image:


FROM postgres:10
COPY src/main/resources/create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/
COPY src/main/resources/create-multiple-postgresql-databases.sql /docker-entrypoint-initdb.d/

Start as follows:


docker run -itd \
    --name pkslow-postgres \
    -e POSTGRES_MULTIPLE_DATABASES=db1,db2 \
    -e POSTGRES_USER=pkslow \
    -e POSTGRES_PASSWORD=pkslow \
    -p 5432:5432 \
    pkslow/postgresql-multiple-databases:1.0-SNAPSHOT

After successful startup, the following databases will be created:


db1,db2,
logdata,orderdata,userdata

4 Summary

This is the solution used in the development and testing phase. In fact, putting the database in a container is not a good choice.

Please check the code:https://github.com/LarryDpk/pkslow-samples

This is the end of this article about creating multiple databases when docker starts PostgreSQL. For more information about docker starting PostgreSQL, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

Basic knowledge of big data

For an emergency exam, I began to make up for the concept of big data without serious and systematic study It involves Hadoop, HBase, spark, Flink, flume, Kafka, sqoop, HDFS, hive, MapReduce, impala, spark SQL, elasticsearch, Yan, hue and cloudera manager. The purpose of this article is to sort out these related knowledge concepts and […]