Metadata-Version: 2.4
Name: sqliac-postgres
Version: 0.1.0
Summary: PostgreSQL adapter for SQLIaC — a Terraform alternative for SQL databases without a state file.
Requires-Python: >=3.12
Description-Content-Type: text/markdown
Requires-Dist: sqliac>=0.2.0
Requires-Dist: pg8000>=1.31

# sqliac-postgres

PostgreSQL adapter for [SQLIaC](https://pypi.org/project/sqliac/) — a Terraform alternative for SQL databases that uses no state file.

This package provides the PostgreSQL database connector and credential handling so SQLIaC can query Postgres's live state and execute DDL to reconcile your declared resource definitions.

## Installation

```bash
pip install sqliac-postgres
```

This also installs `sqliac` (the CLI and core engine) and `pg8000` (a pure-Python PostgreSQL driver).

## Quick Start

### Credentials

SQLIaC looks for Postgres credentials in two places, in order of precedence:

**1. Environment variables** (recommended)

```bash
export POSTGRES_HOST="localhost"
export POSTGRES_USER="my_user"
export POSTGRES_PASSWORD="my_password"
export POSTGRES_DATABASE="my_db"
# Optional:
export POSTGRES_PORT="5432"
export POSTGRES_SSLMODE="require"
```

**2. Configuration file** `.sqliac/provider/credentials.toml`

```toml
[postgres]
host = "localhost"
user = "my_user"
password = "my_password"
database = "my_db"
```

Environment variables take precedence. Run `sqliac init` to scaffold this file.

### Using SQLIaC with Postgres

```bash
# Scaffold a project
sqliac init

# Configure your credentials and edit definitions/
# then preview and apply:

sqliac apply --dry-run
sqliac apply
```

### Provider config

The adapter is registered via the `sqliac.adapters` entry point group. Your `.sqliac/provider/config.toml` should start with a top-level `[postgres]` section:

```toml
[postgres.schema.ddl_command]
create = "CREATE"
alter  = "ALTER"
drop   = "DROP"

[postgres.schema.ddl_context]
object_name = "analytics"
owner = "analytics_owner"
wait_time = 0

[postgres.schema.ddl_context.depends_on]
```

## API

The adapter exposes three classes, all available from the top-level package:

```python
from sqliac_postgres import PostgresAdapter, PostgresCredentials, PostgresConnectionManager
```

| Class | Role |
|-------|------|
| `PostgresAdapter` | Concrete `BaseAdapter` — executes queries and manages connections |
| `PostgresCredentials` | Frozen dataclass — validates and stores `host`, `user`, `password`, `database`, etc. |
| `PostgresConnectionManager` | Thread-safe per-thread connection pool (autocommit, so DDL runs outside transaction blocks) |

All three are registered via the entry point `sqliac.adapters / postgres` and resolved automatically by the SQLIaC CLI. You don't need to import them directly unless building custom tooling.

### Supported credential fields

| Field | Required | Env var |
|-------|----------|---------|
| `host` | Yes | `POSTGRES_HOST` |
| `user` | Yes | `POSTGRES_USER` |
| `password` | Yes | `POSTGRES_PASSWORD` |
| `database` | Yes | `POSTGRES_DATABASE` |
| `port` | No (default 5432) | `POSTGRES_PORT` |
| `sslmode` | No | `POSTGRES_SSLMODE` |

## License

MIT — see the [sqliac](https://pypi.org/project/sqliac/) package for details.
