From 171caea91ee10639b13357378b08265f9edcce9e Mon Sep 17 00:00:00 2001 From: Brian Bjarke Jensen Date: Mon, 27 Oct 2025 21:47:06 +0100 Subject: [PATCH] updated description of database preparation --- README.md | 19 +++++++++++++----- scripts/create_db_user.sh | 42 --------------------------------------- 2 files changed, 14 insertions(+), 47 deletions(-) delete mode 100644 scripts/create_db_user.sh diff --git a/README.md b/README.md index 514187f..63cb7cd 100644 --- a/README.md +++ b/README.md @@ -56,12 +56,21 @@ energy-consumption-ingester/ 4. **Set up PostgreSQL database:** ```bash - # Create database - createdb energy_consumption + # Connect to Postgres14 host + psql -U -h -p - # Create user (optional - run the script) - chmod +x scripts/create_db_user.sh - ./scripts/create_db_user.sh + # Create database user + CREATE USER WITH PASSWORD ; + + # Create database + CREATE DATABASE ; + + # Grant full privileges on database to user + GRANT ALL PRIVILEGES ON DATABASE TO ; + + # (Optional) verify new user and database creation + \q + psql -U -h -p ``` ## Configuration diff --git a/scripts/create_db_user.sh b/scripts/create_db_user.sh deleted file mode 100644 index fbf6453..0000000 --- a/scripts/create_db_user.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/bash -set -e # Exit immediately if a command exits with a non-zero status. - -# --- 1. Set up Connection Variables --- -HOST=$POSTGRES_HOST -PORT=$POSTGRES_PORT -DB_NAME=$NEW_DB_NAME -DB_USER=$NEW_DB_USER -DB_PASSWORD=$NEW_DB_PASSWORD - -# --- 2. Build the SQL Commands --- -# NOTE: The IF NOT EXISTS ensures the script can be run safely multiple times. -SQL_COMMANDS=$(cat <<-END_SQL - -- Create user role with login and password - DO - \$body\$ - BEGIN - IF NOT EXISTS ( - SELECT - FROM pg_catalog.pg_user - WHERE usename = '$DB_USER') THEN - CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASSWORD'; - END IF; - END - \$body\$; - - -- Create database owned by the new user (if it doesn't exist) - SELECT 'CREATE DATABASE $DB_NAME OWNER $DB_USER' - WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '$DB_NAME')\gexec -END_SQL -) - -# --- 3. Execute the SQL Commands --- -echo "Attempting to connect to $HOST to execute SQL..." -echo "$SQL_COMMANDS" | psql \ - --host=$HOST \ - --port=$PORT \ - --username=$POSTGRES_ADMIN_USER \ - --dbname=postgres \ - --set ON_ERROR_STOP=on - -echo "PostgreSQL database '$DB_NAME' and user '$DB_USER' created/verified successfully."