Skip to content

Commit 0c416d0

Browse files
committed
init-muiltidb.sql.template fixes
1 parent 7956b34 commit 0c416d0

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-pguserstrongpassword}
1414
ENV POSTGRES_DB=${POSTGRES_DB:-postgres}
1515
ENV POSTGRES_DB_SIDECARS=${POSTGRES_DB_SIDECARS}
1616

17-
COPY init-multidb.sh /docker-entrypoint-initdb.d/init-multidb.sh
18-
RUN chmod +x /docker-entrypoint-initdb.d/init-multidb.sh
19-
2017
RUN apt-get update && \
2118
apt-get install -y build-essential postgresql-server-dev-17 git gettext && \
2219
git clone --branch v0.7.1 https://github.com/pgvector/pgvector.git && \
2320
cd pgvector && \
2421
make && \
2522
make install
2623

24+
COPY init-multidb.sh /docker-entrypoint-initdb.d/init-multidb.sh
25+
COPY init-multidb.sql.template /docker-entrypoint-initdb.d/init-multidb.sql.template
26+
RUN chmod +x /docker-entrypoint-initdb.d/init-multidb.sh
27+

init-multidb.sh

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,25 @@ function wait_for_postgres() {
99
done
1010
}
1111

12-
# Only run on first init
13-
if [ "$1" = "postgres" ]; then
14-
# Create sidecar DBs and users
15-
IFS=',' read -ra DBS <<< "$POSTGRES_DB_SIDECARS"
16-
for DB in "${DBS[@]}"; do
17-
DB_TRIMMED=$(echo "$DB" | xargs)
18-
if [ -z "$DB_TRIMMED" ]; then continue; fi
19-
DB_LOWER=$(echo "$DB_TRIMMED" | tr '[:upper:]' '[:lower:]')
20-
USER="${DB_LOWER}_user"
21-
PASS="mydefaultpassword"
22-
echo "Creating sidecar DB: $DB_TRIMMED and user: $USER"
23-
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
24-
DO
25-
$$
26-
BEGIN
27-
IF NOT EXISTS (SELECT FROM pg_database WHERE datname = '$DB_TRIMMED') THEN
28-
CREATE DATABASE "$DB_TRIMMED";
29-
END IF;
30-
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '$USER') THEN
31-
CREATE ROLE "$USER" WITH LOGIN PASSWORD '$PASS';
32-
END IF;
33-
GRANT ALL PRIVILEGES ON DATABASE "$DB_TRIMMED" TO "$USER";
34-
END
35-
$$;
36-
EOSQL
37-
# Enable pgvector if VECTOR_ prefix
38-
if [[ "$DB_TRIMMED" =~ ^VECTOR_ ]]; then
39-
echo "Enabling pgvector on $DB_TRIMMED"
40-
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname="$DB_TRIMMED" -c "CREATE EXTENSION IF NOT EXISTS vector;"
41-
fi
42-
done
43-
fi
12+
# Create sidecar DBs and users
13+
IFS=',' read -ra DBS <<< "$POSTGRES_DB_SIDECARS"
14+
for DB in "${DBS[@]}"; do
15+
DB_TRIMMED=$(echo "$DB" | xargs)
16+
if [ -z "$DB_TRIMMED" ]; then continue; fi
17+
DB_LOWER=$(echo "$DB_TRIMMED" | tr '[:upper:]' '[:lower:]')
18+
USER="${DB_LOWER}_user"
19+
PASS="mydefaultpassword"
20+
echo "Creating sidecar DB: $DB_TRIMMED and user: $USER"
21+
22+
# Use the template file and envsubst
23+
export DB_TRIMMED USER PASS
24+
envsubst < /docker-entrypoint-initdb.d/init-multidb.sql.template > /tmp/init-multidb.sql
25+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" -f /tmp/init-multidb.sql
26+
rm -f /tmp/init-multidb.sql
27+
28+
# Enable pgvector if VECTOR_ prefix (case-insensitive)
29+
if [[ "$DB_TRIMMED" =~ ^[Vv][Ee][Cc][Tt][Oo][Rr]_ ]]; then
30+
echo "Enabling pgvector on $DB_TRIMMED"
31+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname="$DB_TRIMMED" -c "CREATE EXTENSION IF NOT EXISTS vector;"
32+
fi
33+
done

init-multidb.sql.template

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Create database if it does not exist
2+
SELECT 'CREATE DATABASE "${DB_TRIMMED}"'
3+
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${DB_TRIMMED}')
4+
\gexec
5+
6+
-- Create user and grant privileges
7+
DO
8+
$$
9+
BEGIN
10+
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = '${USER}') THEN
11+
CREATE ROLE "${USER}" WITH LOGIN PASSWORD '${PASS}';
12+
END IF;
13+
END
14+
$$;
15+
16+
GRANT ALL PRIVILEGES ON DATABASE "${DB_TRIMMED}" TO "${USER}";

0 commit comments

Comments
 (0)