Skip to content

Commit 138f306

Browse files
committed
fix(postgresql): quote identifiers in mkdb.sh ALTER ROLE for dashed names
PostgreSQL treats unquoted hyphenated identifiers as subtraction. Use double-quoted role names and escaped password literals when running ALTER ROLE via psql -c. Made-with: Cursor
1 parent c56eef9 commit 138f306

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

apps/postgresql/bin/mkdb.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
#!/bin/bash
22

3+
# PostgreSQL identifiers (role/database names) must be double-quoted when they contain "-" etc.
4+
psql_ident() {
5+
printf '"%s"' "${1//\"/\"\"}"
6+
}
7+
8+
# Single-quoted SQL string literal (PASSWORD).
9+
psql_literal() {
10+
printf "'%s'" "${1//\'/\'\'}"
11+
}
12+
313
if [ "$#" -ne 1 ]; then
414
echo "Usage: $0 <application name>"
515
exit 1
@@ -65,9 +75,8 @@ fi
6575

6676
echo "3. setting user password"
6777
echo " Setting password for ${USER}..."
68-
if docker compose -f "$COMPOSE_FILE" exec -T postgresql psql -U "${POSTGRES_USER}" -d postgres << EOF
69-
ALTER ROLE ${USER} WITH PASSWORD '${PASSWORD}';
70-
EOF
78+
alter_sql="ALTER ROLE $(psql_ident "$USER") WITH PASSWORD $(psql_literal "$PASSWORD");"
79+
if docker compose -f "$COMPOSE_FILE" exec -T postgresql psql -U "${POSTGRES_USER}" -d postgres -c "$alter_sql"
7180
then
7281
echo " ✓ Password set successfully"
7382
else

0 commit comments

Comments
 (0)