Skip to content

fix: postgres init-databases.sh psql variable syntax error#8

Open
RimSM wants to merge 1 commit into
mainfrom
fix/postgres-init-databases
Open

fix: postgres init-databases.sh psql variable syntax error#8
RimSM wants to merge 1 commit into
mainfrom
fix/postgres-init-databases

Conversation

@RimSM

@RimSM RimSM commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

  • docker/postgres/init-databases.sh의 psql --set 변수 치환 문법(:variable)이 postgres:16 이미지에서 동작하지 않는 버그 수정
  • 이 버그로 인해 airflow_metadata, superset_metadata DB가 초기화 시 생성되지 않아 Airflow/Superset 컨테이너가 시작 불가한 상태가 됨
  • psql 변수 치환 대신 일반 쿼리 문자열 방식으로 교체

Test plan

  • docker compose down -v && docker compose up -d 실행 시 Airflow/Superset이 정상 기동되는지 확인
  • postgres 컨테이너 로그에 init-databases.sh 에러 없는지 확인

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Improved database initialization process with internal optimizations to the initialization script.

Note: This release contains technical infrastructure improvements with no visible changes to end-user functionality.

psql --set 변수 치환 문법(:variable)이 postgres:16 이미지에서 동작하지
않아 airflow_metadata, superset_metadata DB가 생성되지 않는 문제 수정.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The create_database function in the Postgres initialization script is refactored to remove an intermediate variable and inline the database existence check. Database creation now uses a direct SQL statement instead of parameterized psql execution.

Changes

Postgres initialization refactor

Layer / File(s) Summary
create_database function refactor
docker/postgres/init-databases.sh
Database existence is checked inline via psql with grep, and database creation uses a direct CREATE DATABASE "$database" SQL statement, replacing the parameterized psql --set=database=... approach.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A script so small, yet sharp and clean,
Postgres dances, unseen, serene,
No more variables cluttering the way—
SQL speaks clear as dawn's first ray!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately describes the main fix: removing psql variable syntax that was incompatible with postgres:16 and replacing it with a direct query approach.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/postgres-init-databases

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docker/postgres/init-databases.sh`:
- Around line 12-13: Validate and sanitize the database name in the script
before using it in the psql CREATE DATABASE command: ensure the shell variable
database contains only allowed characters (e.g., letters, digits, underscores,
hyphens) or otherwise reject it, and escape any embedded double quotes by
doubling them (replace " with "") before interpolating into the SQL; then use
the same sanitized/escaped variable in the psql --command "CREATE DATABASE
\"$database\"" invocation so the CREATE statement is safe from names containing
quotes or invalid characters.
- Around line 8-10: The psql command in init-databases.sh interpolates $database
directly into SQL (psql ... --command "SELECT ... WHERE datname = '$database'")
creating an SQL injection risk; fix by validating or sanitizing $database before
use: add a check that $database matches a strict pattern (e.g.,
/^[a-zA-Z0-9_]+$/) and exit with error if not, or alternatively construct the
SQL using safe dollar-quoting (e.g., WHERE datname = $$<database>$$) or properly
escape single quotes, then use the validated/sanitized $database in the psql
--command invocation to ensure no untrusted characters can break out of the
string.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c5f0bf87-b0f2-434a-9c27-eb210a87c2f3

📥 Commits

Reviewing files that changed from the base of the PR and between 18033b7 and bbcd269.

📒 Files selected for processing (1)
  • docker/postgres/init-databases.sh

Comment on lines +8 to +10
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres \
--command "SELECT 1 FROM pg_database WHERE datname = '$database'" \
--tuples-only --no-align | grep -q 1 && return 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | 🏗️ Heavy lift

SQL injection vulnerability in database existence check.

The $database variable is directly interpolated into the SQL WHERE clause using single quotes: datname = '$database'. If the database name contains a single quote character, it can break out of the string literal and execute arbitrary SQL.

Example attack vector:

KOIN_DATA_AIRFLOW_DB="'; DROP DATABASE postgres; --"

Would result in:

SELECT 1 FROM pg_database WHERE datname = ''; DROP DATABASE postgres; --'

While this script runs in a trusted container initialization context with operator-controlled environment variables, it's still a security posture gap that should be addressed.

🛡️ Proposed fix with input validation

Add input validation to ensure database names only contain safe characters:

 create_database() {
   local database="$1"
+
+  # Validate database name (alphanumeric, underscore only)
+  if ! [[ "$database" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
+    echo "Error: Invalid database name '$database'" >&2
+    return 1
+  fi
 
   psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres \

Alternatively, use PostgreSQL's dollar-quoted strings to avoid single-quote escaping issues:

   psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres \
-    --command "SELECT 1 FROM pg_database WHERE datname = '$database'" \
+    --command "SELECT 1 FROM pg_database WHERE datname = \$\$${database}\$\$" \
     --tuples-only --no-align | grep -q 1 && return 0
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docker/postgres/init-databases.sh` around lines 8 - 10, The psql command in
init-databases.sh interpolates $database directly into SQL (psql ... --command
"SELECT ... WHERE datname = '$database'") creating an SQL injection risk; fix by
validating or sanitizing $database before use: add a check that $database
matches a strict pattern (e.g., /^[a-zA-Z0-9_]+$/) and exit with error if not,
or alternatively construct the SQL using safe dollar-quoting (e.g., WHERE
datname = $$<database>$$) or properly escape single quotes, then use the
validated/sanitized $database in the psql --command invocation to ensure no
untrusted characters can break out of the string.

Comment on lines +12 to +13
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres \
--command "CREATE DATABASE \"$database\""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Database creation uses safer identifier quoting but still benefits from input validation.

The double-quoted identifier syntax (CREATE DATABASE "$database") is more injection-resistant than the single-quoted string literal in the existence check above. However, database names containing double quotes would still require proper escaping ("" in PostgreSQL).

The input validation suggested in the previous comment (lines 8-10) would also protect this statement.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docker/postgres/init-databases.sh` around lines 12 - 13, Validate and
sanitize the database name in the script before using it in the psql CREATE
DATABASE command: ensure the shell variable database contains only allowed
characters (e.g., letters, digits, underscores, hyphens) or otherwise reject it,
and escape any embedded double quotes by doubling them (replace " with "")
before interpolating into the SQL; then use the same sanitized/escaped variable
in the psql --command "CREATE DATABASE \"$database\"" invocation so the CREATE
statement is safe from names containing quotes or invalid characters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant