docs: align postgres env examples and grants

This commit is contained in:
Ilgiz Kalmetyev 2026-04-09 22:47:07 +05:00
parent a8c5c3d500
commit 1a67164431
3 changed files with 66 additions and 17 deletions

View File

@ -14,7 +14,7 @@ SERVICE_PROXY_HEADERS=true
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=techlib
DATABASE_USER=techlib
DATABASE_USER=techlib_app
DATABASE_PASSWORD=
DATABASE_SSLMODE=prefer

View File

@ -14,7 +14,7 @@ SERVICE_PROXY_HEADERS=true
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=techlib
DATABASE_USER=techlib
DATABASE_USER=techlib_app
DATABASE_PASSWORD=
DATABASE_SSLMODE=require

View File

@ -2,10 +2,14 @@
This project uses PostgreSQL 15+ as the authoritative state backend.
The guidance below is intentionally practical and aligned with the current codebase.
The example environment files mirror the configuration model:
- `.env.example` uses runtime `APP_*` and `DATABASE_*` variables for the service role.
- `.env.test.example` uses `TEST_*` variables for isolated test execution.
## Database Creation
Create a dedicated database and roles. A simple split is:
Create a dedicated database, schema, and roles. A minimal split is:
- one role for migrations
- one role for runtime access
@ -19,10 +23,63 @@ CREATE ROLE techlib_app LOGIN PASSWORD 'change-me';
CREATE ROLE techlib_test LOGIN PASSWORD 'change-me';
CREATE DATABASE techlib OWNER techlib_migrator;
CREATE DATABASE test_techlib OWNER techlib_test;
CREATE DATABASE test_techlib OWNER techlib_migrator;
```
If you use a separate schema owner, grant the minimum required privileges instead of using superuser.
The application schema should be owned by the migrator role. If you use a
different schema name, replace `techlib` consistently in the examples below.
## Minimal Grants
The following SQL is the smallest practical privilege set for this project.
Run the relevant block in each database after connecting as a superuser or
source database owner.
### Runtime Database
```sql
\connect techlib
CREATE SCHEMA IF NOT EXISTS techlib AUTHORIZATION techlib_migrator;
GRANT CONNECT ON DATABASE techlib TO techlib_migrator, techlib_app;
GRANT USAGE, CREATE ON SCHEMA techlib TO techlib_migrator;
GRANT USAGE ON SCHEMA techlib TO techlib_app;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA techlib TO techlib_app;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA techlib TO techlib_app;
ALTER DEFAULT PRIVILEGES FOR ROLE techlib_migrator IN SCHEMA techlib
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO techlib_app;
ALTER DEFAULT PRIVILEGES FOR ROLE techlib_migrator IN SCHEMA techlib
GRANT USAGE, SELECT ON SEQUENCES TO techlib_app;
```
### Test Database
```sql
\connect test_techlib
CREATE SCHEMA IF NOT EXISTS techlib AUTHORIZATION techlib_migrator;
GRANT CONNECT ON DATABASE test_techlib TO techlib_migrator, techlib_test;
GRANT USAGE, CREATE ON SCHEMA techlib TO techlib_migrator;
GRANT USAGE ON SCHEMA techlib TO techlib_test;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA techlib TO techlib_test;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA techlib TO techlib_test;
ALTER DEFAULT PRIVILEGES FOR ROLE techlib_migrator IN SCHEMA techlib
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO techlib_test;
ALTER DEFAULT PRIVILEGES FOR ROLE techlib_migrator IN SCHEMA techlib
GRANT USAGE, SELECT ON SEQUENCES TO techlib_test;
```
These grants are intentionally explicit:
- `techlib_migrator` owns schema changes and runs Alembic.
- `techlib_app` is the runtime read/write role for the service.
- `techlib_test` is isolated to the `test_` database and never points at the runtime database.
## Migration Commands
@ -63,6 +120,7 @@ python -m techlib_mcpserver
```
The runtime storage path is resolved relative to `WORKING_DIRECTORY` when it is not absolute.
The example `.env.example` file follows the same naming and role split.
## Test DB Usage
@ -84,6 +142,7 @@ python -m pytest -q
```
The repository includes a strict pytest guard that fails if a test tries to use a non-test PostgreSQL URL.
The `.env.test.example` file mirrors the `TEST_`-prefixed variables.
## Backup and Restore
@ -106,18 +165,8 @@ For production, back up both the database and the document storage directory tha
## Least Privilege
Recommended role split:
- `techlib_migrator`: owns schema changes and runs Alembic
- `techlib_app`: runtime read/write access for the service
- `techlib_test`: isolated test database access
Avoid superuser privileges for the application. Grant only the permissions needed for the role's job:
- `CONNECT` on the database
- `USAGE` on the schema
- table-level `SELECT`, `INSERT`, `UPDATE`, `DELETE` for the app role
- sequence `USAGE` where needed
Avoid superuser privileges for the application.
The grant blocks above are the minimum needed for this repository's schema and test layout.
## Operational Notes