updated readme with description of read-only user for database integration with home assistant
This commit is contained in:
@@ -70,7 +70,66 @@ energy-consumption-ingester/
|
|||||||
|
|
||||||
# (Optional) verify new user and database creation
|
# (Optional) verify new user and database creation
|
||||||
\q
|
\q
|
||||||
psql -U <DB_USER> -h <POSTGRES_HOST> -p <POSTGERS_PORT>
|
psql -U <DB_USER> -h <POSTGRES_HOST> -p <POSTGRES_PORT>
|
||||||
|
```
|
||||||
|
|
||||||
|
5. **OPTIONAL - Add read-only user to database:**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Connect to PostgreSQL as admin user
|
||||||
|
psql -U <POSTGRES_ADMIN> -h <POSTGRES_HOST> -p <POSTGRES_PORT> -d <DB_NAME>
|
||||||
|
|
||||||
|
# Create read-only user
|
||||||
|
CREATE USER <READONLY_USER> WITH PASSWORD '<READONLY_PASSWORD>';
|
||||||
|
|
||||||
|
# Grant connect privilege to the database
|
||||||
|
GRANT CONNECT ON DATABASE <DB_NAME> TO <READONLY_USER>;
|
||||||
|
|
||||||
|
# Grant usage on the schema (assuming default 'public' schema)
|
||||||
|
GRANT USAGE ON SCHEMA public TO <READONLY_USER>;
|
||||||
|
|
||||||
|
# Grant select privileges on all existing tables
|
||||||
|
GRANT SELECT ON ALL TABLES IN SCHEMA public TO <READONLY_USER>;
|
||||||
|
|
||||||
|
# Grant select privileges on all future tables (important for new tables)
|
||||||
|
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO <READONLY_USER>;
|
||||||
|
|
||||||
|
# Grant usage on all existing sequences (needed for serial columns)
|
||||||
|
GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO <READONLY_USER>;
|
||||||
|
|
||||||
|
# Grant usage on all future sequences
|
||||||
|
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE ON SEQUENCES TO <READONLY_USER>;
|
||||||
|
|
||||||
|
# Verify the read-only user can connect and query
|
||||||
|
\q
|
||||||
|
psql -U <READONLY_USER> -h <POSTGRES_HOST> -p <POSTGRES_PORT> -d <DB_NAME>
|
||||||
|
|
||||||
|
# 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://<READONLY_USER>:<READONLY_PASSWORD>@<POSTGRES_HOST>:<POSTGRES_PORT>/<DB_NAME>
|
||||||
|
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
|
## Configuration
|
||||||
|
|||||||
Reference in New Issue
Block a user