# PostgreSQL

Source: https://docs.formance.com/v4.0/deploy/self-hosted/infrastructure/postgresql

Stack: v4.0

_Version 14 or higher is required._

Ledger does not use PostgreSQL in this release

Ledger 3.0 stores its data in an embedded store on each node and replicates through Raft, so it has no PostgreSQL database and no database URI setting of its own. Size and operate its storage with [Plan Capacity and Tune Performance](https://docs.formance.com/v4.0/modules/ledger/operations/capacity-and-performance) instead; the background is in [How Ledger Stores and Replicates Data](https://docs.formance.com/v4.0/modules/ledger/get-started/architecture).

Everything below applies to the modules that still use PostgreSQL.

## Bundled PostgreSQL

The unified `formance` Helm chart includes a [Bitnami PostgreSQL](https://github.com/bitnami/charts/tree/main/bitnami/postgresql) instance enabled by default:

```yaml
postgresql:
  enabled: true  # default
```

This deploys a single-node PostgreSQL server in your `formance-system` namespace. It's useful for development and testing, but **not recommended for production** workloads.

The bundled instance uses the credentials defined in `global.postgresql.auth`:

```yaml
global:
  postgresql:
    host: "postgresql"
    auth:
      username: formance
      password: formance
      database: formance
```

The bundled PostgreSQL does not include persistent storage by default. Do not rely on it for production data.

Portal and Console-v3 do not use `global.postgresql.auth.database`: each app needs its own database. See [Portal and Console Databases](#portal-and-console-databases).

## External PostgreSQL

For production, use a managed PostgreSQL service and disable the bundled instance:

```yaml
postgresql:
  enabled: false

global:
  postgresql:
    host: "my-postgres.example.com"
    additionalArgs: "sslmode=require"
    auth:
      username: "formance"
      password: "your-secure-password"
      database: "formance"
      existingSecret: ""  # or reference a Kubernetes secret
      secretKeys:
        adminPasswordKey: ""
        userPasswordKey: ""
    service:
      ports:
        postgresql: 5432
```

Use `global.postgresql.auth.existingSecret` to reference a Kubernetes Secret instead of storing passwords in your values file.

## Portal and Console Databases

Portal and Console-v3 each need their own database, separate from the other app and from `global.postgresql.auth.database`. If both apps share a database, the migrations of one app collide with the migrations of the other, and users cannot log in.

The `formance` chart has given each app its own database since its first version, so `formance` chart installs are not affected. The problem only affects the `cloudprem` chart installed on its own, before version 6.4.2: these versions sent both apps to `global.postgresql.auth.database`. From cloudprem 6.4.2, `portal` and `console` are the defaults.

Set the databases explicitly anyway, so that your values file does not depend on the chart version. With the `formance` chart:

```yaml title="values.yaml (formance chart)"
cloudprem:
  portal:
    config:
      postgresql:
        auth:
          database: portal
  console-v3:
    config:
      postgresql:
        auth:
          database: console
```

With the `cloudprem` chart installed on its own, the keys are at the root:

```yaml title="values.yaml (cloudprem chart)"
portal:
  config:
    postgresql:
      auth:
        database: portal
console-v3:
  config:
    postgresql:
      auth:
        database: console
```

### Database Prerequisites

If the databases set in `auth.database` don't exist, the migration jobs create them, which needs the `CREATEDB` privilege. Otherwise, create them beforehand, owned by the application user. With the values above:

```sql
CREATE DATABASE portal OWNER <user>;
CREATE DATABASE console OWNER <user>;
```

Up to v4.1.1, the migration jobs also require these exact names, whatever `auth.database` says, and `CONNECT` on the `postgres` database.

### Upgrade from Portal and Console-v3 1.x

Portal and Console-v3 store their sessions in PostgreSQL since v2.0.0, and their migration jobs look for the `portal` and `console` databases since v2.6.0. If you upgrade a standalone cloudprem install from 1.x, set the two databases above and meet the [prerequisites](#database-prerequisites) before you upgrade.

### Recover from a Shared Database

If both apps used one database, you see one of these symptoms:

- The migration job fails with `permission denied to create database` (v4.1.1 and earlier).
- The migration job succeeds, but login fails with `Failed query: insert into "session" ... on conflict ("id","organization_id")`.
- From v4.2.0, the migration job fails with `Database "<name>" already holds another application's migrations`. The job stops before it writes anything.

To recover, point each app at its own database and upgrade again. The migrations run from scratch in the new databases, and users must log in again.

Keep `global.postgresql.auth.database` as it is: Membership still uses it.

To keep the data of the app that worked on the shared database, and to clean up that database, follow [Recovering from a shared database](https://github.com/formancehq/helm/tree/main/charts/cloudprem#recovering-from-a-shared-database) in the cloudprem chart README.

## Configure PostgreSQL URI via Helm Values

The Formance Operator uses `Settings` CRDs to configure database connections for each stack. You can create these Settings declaratively through Helm values using `regions.settings`:

```yaml
regions:
  settings:
    postgres-uri:
      key: "postgres.*.uri"
      value: "postgresql://formance:formance@my-postgres.example.com:5432?disableSSLMode=true"
```

This creates a `Settings` resource that applies the PostgreSQL URI to **all stacks** and **all modules** (the `*` wildcard).

### Scope to Specific Stacks

To target a specific stack, add a `stacks` field:

```yaml
regions:
  settings:
    postgres-uri:
      key: "postgres.*.uri"
      stacks:
        - "my-stack"
      value: "postgresql://formance:formance@my-postgres.example.com:5432?disableSSLMode=true"
```

### Per-Module Configuration

Use the module name instead of `*` to configure different PostgreSQL servers per module:

```yaml
regions:
  settings:
    postgres-auth:
      key: "postgres.auth.uri"
      value: "postgresql://formance:formance@pg-auth.example.com:5432?disableSSLMode=true"
```

## Create Database Settings Manually

If you prefer to manage Settings CRDs directly with `kubectl`, you can create them manually.

### Option 1: Use the same server for all modules

Set up a PostgreSQL cluster for all modules of the `formance-dev` stack. Each module gets its own database, named `{stackName}-{module}`.

The database name uses the format `{stackName}-{module}`.

```yaml
apiVersion: formance.com/v1beta1
kind: Settings
metadata:
  name: formance-dev-postgres-uri
spec:
  key: postgres.*.uri
  stacks:
    - 'formance-dev'
  value: postgresql://formance:formance@postgresql.formance-system.svc:5432?disableSSLMode=true
```

### Option 2: Use different servers for each module

```yaml
---
apiVersion: formance.com/v1beta1
kind: Settings
metadata:
  name: formance-dev-auth-postgres-uri
spec:
  key: postgres.auth.uri
  stacks:
    - 'formance-dev'
  value: postgresql://formance:formance@postgresql-auth.formance-system.svc:5432?disableSSLMode=true
```

### Option 3: Use PostgreSQL on AWS RDS with an IAM role

```yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: aws-rds-access-role
  namespace: formance-system
  labels:
    formance.com/stack: any
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::AWS_ACCOUNT_ID:role/AWS_ROLE_NAME
---
apiVersion: formance.com/v1beta1
kind: Settings
metadata:
  name: formance-dev-aws-service-account
spec:
  stacks:
    - formance-dev
  key: aws.service-account
  value: aws-rds-access-role
---
apiVersion: formance.com/v1beta1
kind: Settings
metadata:
  name: formance-dev-postgres-uri
spec:
  key: postgres.*.uri
  stacks:
    - 'formance-dev'
  value: postgresql://formance@postgresql.formance-system.svc:5432
```
