From 20d46bb743f222c2964a08238c4845b668d36d3a Mon Sep 17 00:00:00 2001 From: Brian Bjarke Jensen Date: Mon, 27 Oct 2025 22:28:05 +0100 Subject: [PATCH] updated readme with description of read-only user for database integration with home assistant --- README.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 63cb7cd..7baef02 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,66 @@ energy-consumption-ingester/ # (Optional) verify new user and database creation \q - psql -U -h -p + psql -U -h -p + ``` + +5. **OPTIONAL - Add read-only user to database:** + + ```bash + # Connect to PostgreSQL as admin user + psql -U -h -p -d + + # Create read-only user + CREATE USER WITH PASSWORD ''; + + # Grant connect privilege to the database + GRANT CONNECT ON DATABASE TO ; + + # Grant usage on the schema (assuming default 'public' schema) + GRANT USAGE ON SCHEMA public TO ; + + # Grant select privileges on all existing tables + GRANT SELECT ON ALL TABLES IN SCHEMA public TO ; + + # Grant select privileges on all future tables (important for new tables) + ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO ; + + # Grant usage on all existing sequences (needed for serial columns) + GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO ; + + # Grant usage on all future sequences + ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE ON SEQUENCES TO ; + + # Verify the read-only user can connect and query + \q + psql -U -h -p -d + + # Test read access (should work) + SELECT COUNT(*) FROM metering_points; + SELECT COUNT(*) FROM consumption_readings; + + # Test write access (should fail) + INSERT INTO metering_points (metering_point_id) VALUES ('test'); -- Should get permission denied + ``` + + **For Home Assistant integration**, use these read-only credentials in your `configuration.yaml`: + + ```yaml + # configuration.yaml + sensor: + - platform: sql + db_url: postgresql://:@:/ + queries: + - name: "Latest Energy Reading" + query: > + SELECT + consumption_kwh, + timestamp + FROM consumption_readings + ORDER BY timestamp DESC + LIMIT 1 + column: consumption_kwh + unit_of_measurement: "kWh" ``` ## Configuration