Skip to content

Commit 5087a65

Browse files
committed
feat: Initial commit
0 parents  commit 5087a65

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

.github/dependabot.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: pip
4+
directory: "/.github/workflows"
5+
schedule:
6+
interval: daily
7+
commit-message:
8+
prefix: "ci: "
9+
- package-ecosystem: github-actions
10+
directory: "/"
11+
schedule:
12+
interval: "daily"
13+
commit-message:
14+
prefix: "ci: "

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ARG POSTGRES_TAG=latest
2+
FROM postgres:${POSTGRES_TAG}
3+
4+
COPY create-multiple-databases.sh /docker-entrypoint-initdb.d/

README

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<h1 align="center">docker-postgres-multiple-databases 👑</h1>
2+
3+
**Use multiple databases with the official [PostgreSQL Docker Image](https://hub.docker.com/_/postgres/).**
4+
5+
## How To Use 💡
6+
7+
This repository extends the official PostgreSQL Docker Image by adding an additional environment variable `POSTGRES_MULTIPLE_DATABASES`.
8+
9+
Additional databases can be passed as a ***comma-separated list*** in different formats:
10+
11+
### Database, username and password (`database:username:password`):
12+
13+
```yaml
14+
postgres:
15+
image: eu.gcr.io/your-project/postgres-multi-db
16+
environment:
17+
- POSTGRES_MULTIPLE_DATABASES=database1:username1:password1,database2:username2,password2
18+
- POSTGRES_USER=username
19+
- POSTGRES_PASSWORD=password
20+
- POSTGRES_DB=database
21+
```
22+
23+
### Database and username (`database:username`)
24+
25+
```yaml
26+
postgres:
27+
image: eu.gcr.io/your-project/postgres-multi-db
28+
environment:
29+
- POSTGRES_MULTIPLE_DATABASES=database1:username1,database2:username2
30+
- POSTGRES_USER=username
31+
- POSTGRES_PASSWORD=password
32+
- POSTGRES_DB=database
33+
```
34+
35+
Passwords will default to the value of `POSTGRES_PASSWORD`.
36+
37+
### Database (`database`)
38+
39+
```yaml
40+
postgres:
41+
image: eu.gcr.io/your-project/postgres-multi-db
42+
environment:
43+
- POSTGRES_MULTIPLE_DATABASES=database1,database2
44+
- POSTGRES_USER=username
45+
- POSTGRES_PASSWORD=password
46+
- POSTGRES_DB=database
47+
```
48+
49+
Usernames and passwords will default to the values of `POSTGRES_USER` and `POSTGRES_PASSWORD`.
50+
51+
### Mixed
52+
53+
```yaml
54+
postgres:
55+
image: eu.gcr.io/your-project/postgres-multi-db
56+
environment:
57+
- POSTGRES_MULTIPLE_DATABASES=database1:username1:password1,database2:username2,database3
58+
- POSTGRES_USER=username
59+
- POSTGRES_PASSWORD=password
60+
- POSTGRES_DB=database
61+
```
62+
63+
Missing usernames and passwords will default to the values of `POSTGRES_USER` and `POSTGRES_PASSWORD`.

create-multiple-databases.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -u
5+
6+
function create_user_and_database() {
7+
local dbinfo=$1
8+
IFS=":" read -r database user password <<<"$dbinfo"
9+
echo "Creating database '$database' with user '$user'."
10+
11+
userExists=$(psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 1 FROM pg_user WHERE usename = '$user';")
12+
if [[ "$userExists" == *"(0 rows)"* ]]; then
13+
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL
14+
CREATE USER "$user";
15+
ALTER USER "$user" WITH ENCRYPTED PASSWORD '$password';
16+
EOSQL
17+
fi
18+
19+
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL
20+
CREATE DATABASE "$database";
21+
GRANT ALL PRIVILEGES ON DATABASE "$database" TO "$user";
22+
EOSQL
23+
}
24+
25+
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
26+
echo "Multiple database creation requested."
27+
for entry in $(echo "$POSTGRES_MULTIPLE_DATABASES" | tr ',' ' '); do
28+
entry=$(echo "$entry" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
29+
30+
num_colons=$(grep -o ":" <<<"$entry" | wc -l)
31+
32+
if [ "$num_colons" -eq 0 ]; then
33+
entry="$entry:$POSTGRES_USER:$POSTGRES_PASSWORD"
34+
elif [ "$num_colons" -eq 1 ]; then
35+
entry="$entry:$POSTGRES_PASSWORD"
36+
fi
37+
create_user_and_database "$entry"
38+
done
39+
echo "Multiple databases created."
40+
fi

0 commit comments

Comments
 (0)