# ServiceControl PostgreSQL Storage From ServiceControl 7, Error and Audit instances can use PostgreSQL instead of [RavenDB storage](/servicecontrol/storage/ravendb/index.md). Select PostgreSQL when an instance should use an existing PostgreSQL deployment. Storage is configured separately for each instance. > [!IMPORTANT] > When using PostgreSQL, Error instances require [external message body storage](/servicecontrol/storage/external-body/index.md) in addition to the database, even when message bodies fit inline. Audit instances start without it, but then [do not store large or binary bodies](/servicecontrol/storage/external-body/index.md#storage-providers-servicecontrol-auditmessagebodystoragetype). ## Supported PostgreSQL versions ServiceControl supports the PostgreSQL major versions that are currently supported by the PostgreSQL Global Development Group; see the [PostgreSQL versioning policy](https://www.postgresql.org/support/versioning/). Use the latest minor release of the chosen major version. PostgreSQL 18 or later is recommended. In testing by Particular Software, PostgreSQL 18 significantly improved ServiceControl performance. PostgreSQL 18 can [build GIN indexes in parallel](https://www.postgresql.org/docs/18/release-18.html), and ServiceControl uses GIN indexes for full-text message search. ## Prepare the database Create a database before configuring the instance as ServiceControl does not create the database. Setup creates the instance's schema if it does not exist. If the identity that runs setup cannot create schemas, create the schema in advance and grant that identity rights on it. Error and Audit instances can use the same database but not the same schema; see [sharing a database](/servicecontrol/storage/index.md#sharing-a-database). PostgreSQL full-text search uses a built-in `tsvector` GIN index and message body search is always enabled for PostgreSQL storage, regardless of the [`EnableFullTextSearchOnBodies`](/servicecontrol/servicecontrol-instances/configuration.md#ravendb-storage-servicecontrolenablefulltextsearchonbodies) setting. ## Database permissions Grant each role the following rights: | Role | Required rights | | --- | --- | | The role that runs setup | `CREATE` on the database, so that setup can create the schema. If the schema already exists, `USAGE` and `CREATE` on it instead. Setup creates the tables and indexes, and for Audit instances the first daily partitions, so this role owns them. | | The role an Error instance runs as | `USAGE` on the schema, and `SELECT`, `INSERT`, `UPDATE`, and `DELETE` on the tables in it. No sequence privileges are needed. | | The role an Audit instance runs as | Ownership of the Audit tables: the same role that runs setup, or a member of it. See the warning below. | An Error instance with no `Database/Schema` uses the connection's current schema, usually `public`. From PostgreSQL 15, only the database owner can create objects in `public` unless `CREATE` on it is granted, so the setup role needs `CREATE` on `public` or ownership of the database. When the same connection string is used for setup and normal operation, as the ServiceControl Management Utility and PowerShell do, that role needs the setup rights. > [!WARNING] > While running, Audit instances create daily partitions up to 48 hours ahead and drop expired ones. PostgreSQL only lets the owner of a [partitioned table](https://www.postgresql.org/docs/current/ddl-partitioning.html) create partitions of it. Setup creates the Audit tables, so they are owned by the role that runs setup. Run setup and normal operation as the same role, or grant the setup role to the runtime role so that the runtime role counts as the owner. Otherwise the **Audit partition provisioning** custom check fails, and Audit ingestion stops when the provisioned partitions run out, within about two days of the last successful provisioning. ## Authentication Configure database authentication in the connection string for each Error or Audit instance. This is separate from the transport connection string. ServiceControl uses [Npgsql](https://www.npgsql.org/doc/connection-string-parameters.html). Methods configured entirely in the connection string work, such as a user name and password, a password file (`Passfile`), a client certificate, or Kerberos. ### Token authentication Azure Database for PostgreSQL and Amazon RDS for PostgreSQL, including Aurora PostgreSQL, can sign in with short-lived access tokens instead of a password. To use them, add `Token Authentication` to the connection string, set `Username` to the database role the token signs in as, and leave out `Password` and `Passfile`. | `Token Authentication` value | Database | Credentials | | --- | --- | --- | | `AzureEntraId` | Azure Database for PostgreSQL with Microsoft Entra authentication | The Azure SDK's `DefaultAzureCredential`. See [Microsoft Entra ID](#authentication-token-authentication-microsoft-entra-id). | | `AwsRdsIam` | Amazon RDS for PostgreSQL or Aurora PostgreSQL with IAM database authentication | The AWS SDK default credential chain, such as an IAM role. The region comes from the `AWS_REGION` environment variable or the AWS profile. | ```text Host=my-server.postgres.database.azure.com;Database=ServiceControl;Username=servicecontrol-identity;Token Authentication=AzureEntraId;SSL Mode=Require Host=my-cluster.cluster-xxxxxxxxxxxx.us-east-1.rds.amazonaws.com;Database=ServiceControl;Username=servicecontrol;Token Authentication=AwsRdsIam;SSL Mode=Require ``` ServiceControl uses a token each time it opens a new database connection: - **AWS IAM:** ServiceControl signs a new token for every connection. - **Microsoft Entra ID:** ServiceControl reuses a token until two minutes before it expires, then gets a new one. If getting a new token fails while the current one is still valid, ServiceControl logs a warning, keeps using the current token, and tries again 10 seconds later. If ServiceControl cannot get a valid token, opening the connection fails with `Could not get a database access token for user on host , database .` The inner exception contains the error from the credential. Neither the error nor the warning includes the token. The same keywords work in the [PostgreSQL transport connection string](/servicecontrol/transports.md#postgresql). #### Microsoft Entra ID ServiceControl gets Microsoft Entra tokens from the Azure SDK's [`DefaultAzureCredential`](https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication/credential-chains#defaultazurecredential-overview). It supports the following identities: | Identity | Configuration | | --- | --- | | System-assigned managed identity | None | | User-assigned managed identity | Set `AZURE_CLIENT_ID` to the client ID of the identity. | | Service principal with a client secret | Set `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, and `AZURE_CLIENT_SECRET`. | | Service principal with a certificate | Set `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, and `AZURE_CLIENT_CERTIFICATE_PATH` to a PEM or PFX file that contains the certificate and its private key. For a password-protected PFX file, also set `AZURE_CLIENT_CERTIFICATE_PASSWORD`. | | Microsoft Entra Workload ID, for example on Azure Kubernetes Service | The environment variables that the workload identity webhook sets. | Set these environment variables for the ServiceControl process. See [`EnvironmentCredential`](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.environmentcredential) and [`ManagedIdentityCredential`](https://learn.microsoft.com/en-us/dotnet/api/azure.identity.managedidentitycredential) for the complete list of variables. `DefaultAzureCredential` tries a chain of credentials in order and uses the first one that returns a token: environment variables (service principals), workload identity, managed identity, and then developer tools such as Visual Studio, the Azure CLI, and Azure PowerShell. On a machine where more than one credential is available, such as a virtual machine with a managed identity where a user is also signed in to the Azure CLI, the first available credential in the chain is used. To use one credential only, set the `AZURE_TOKEN_CREDENTIALS` environment variable to its name, for example `ManagedIdentityCredential` or `EnvironmentCredential`. To exclude developer tool credentials, set it to `prod`. Setting `AZURE_TOKEN_CREDENTIALS` is recommended in production: the instance always signs in as the expected identity, and an authentication error comes from that credential instead of from the whole chain. See [use a specific credential](https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication/credential-chains#use-a-specific-credential) for the supported values. > [!NOTE] > These environment variables apply to the whole ServiceControl process. They also affect other components of the instance that use `DefaultAzureCredential`, such as [Azure Blob Storage message body storage](/servicecontrol/storage/external-body/azure-blob.md) with a service URI. ServiceControl works out the token scope from host names in the Azure public cloud, Azure Government, and Azure China. For other host names, such as a custom DNS alias, add `Token Scope` with the server's Microsoft Entra scope, for example `Token Scope=https://ossrdbms-aad.database.windows.net/.default`. #### AWS IAM The connection string must list exactly one host, because an IAM token is signed for a single host and port. Grant the database user the `rds_iam` role, and give the identity that ServiceControl runs as the `rds-db:connect` permission for that user. A user with `rds_iam` can no longer sign in with a password. ## Error instance configuration Set the storage type to `PostgreSQL`, provide the database connection string, and configure the retention period. Also configure an [external message body store](/servicecontrol/storage/external-body/index.md), which is required even if most bodies fit inline in the database. The ServiceControl Management Utility and the PowerShell module configure it when they create the instance. For manual or container deployments, add the `ServiceControl/MessageBody/*` settings to the instance's configuration file or environment. ### ServiceControl/StorageType _Added in version 7.0.0_ The storage implementation used by the instance. This setting is required; an instance without it does not start. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_STORAGETYPE` | | **App config key** | `ServiceControl/StorageType` | | **SCMU field** | `STORAGE TYPE` | | Type | Default value | | --- | --- | | string | None (required) | Valid values are `RavenDB`, `SQLServer`, and `PostgreSQL`. To choose where message bodies are stored with SQL Server or PostgreSQL storage, use [`ServiceControl/MessageBody/StorageType`](/servicecontrol/servicecontrol-instances/configuration.md#message-body-storage-servicecontrolmessagebodystoragetype). ### ServiceControl/Database/ConnectionString _Added in version 7.0.0_ The connection string for the SQL Server or PostgreSQL database. The database must already exist. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_DATABASE_CONNECTIONSTRING` | | **App config key** | `ServiceControl/Database/ConnectionString` | | **SCMU field** | `DATABASE CONNECTION STRING` | | Type | Default value | | --- | --- | | string | None (required for SQL Server and PostgreSQL storage) | ### ServiceControl/Database/Schema _Added in version 7.0.0_ The schema that contains the Error instance tables. If the schema does not exist, setup creates it, which requires permission to create schemas. Changing this setting does not move existing data; an instance that already has data must keep using the schema that contains it. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_DATABASE_SCHEMA` | | **App config key** | `ServiceControl/Database/Schema` | | **SCMU field** | N/A | | Type | Default value | | --- | --- | | string | None. Tables are created in the connection's default schema: the login's default schema on SQL Server (usually `dbo`), or the current schema from `search_path` on PostgreSQL (usually `public`). | ### ServiceControl/ErrorRetentionPeriod The grace period that errored messages are kept before they are deleted. For a message to be considered for deletion, it needs to have a status of either `Archived`, `RetryIssued`, or `Resolved`. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_ERRORRETENTIONPERIOD` | | **App config key** | `ServiceControl/ErrorRetentionPeriod` | | **SCMU field** | `ERROR RETENTION PERIOD` | | Type | Default value | | --- | --- | | timespan | None (required) (SCMU slider defaults to `15 Days`) | Valid range for this setting is between 5 days and 45 days. Set these variables in the Error instance's environment to use file system body storage. Replace the connection string placeholder with credentials supplied by the deployment environment. These storage settings supplement the instance's transport and other required settings. ```text SERVICECONTROL_STORAGETYPE=PostgreSQL SERVICECONTROL_DATABASE_CONNECTIONSTRING="Host=postgres;Database=ServiceControl;Username=sc;Password=..." SERVICECONTROL_MESSAGEBODY_STORAGETYPE=FileSystem SERVICECONTROL_MESSAGEBODY_FILESYSTEM_STORAGEPATH=/var/servicecontrol/bodies SERVICECONTROL_ERRORRETENTIONPERIOD=15 ``` See the [Error instance settings reference](/servicecontrol/servicecontrol-instances/configuration.md#sql-server-and-postgresql-storage) for the complete reference. To share the ingestion load across several processes, see [Scaling out error ingestion](/servicecontrol/servicecontrol-instances/scaling-out-ingestion.md). Workers use this instance's database, schema, and message body store, and only this instance runs setup. ## Audit instance configuration Set the storage type to `PostgreSQL`, provide the database connection string, and configure the retention period. Also configure an [external message body store](/servicecontrol/storage/external-body/index.md), which is required even if most bodies fit inline in the database. The ServiceControl Management Utility and the PowerShell module configure it when they create the instance. For manual or container deployments, add the `ServiceControl.Audit/MessageBody/*` settings to the instance's configuration file or environment. ### ServiceControl.Audit/StorageType _Added in version 7.0.0_ The storage implementation used by the Audit instance. This setting is required; an instance without it does not start. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_AUDIT_STORAGETYPE` | | **App config key** | `ServiceControl.Audit/StorageType` | | **SCMU field** | `STORAGE TYPE` | | Type | Default value | | --- | --- | | string | None (required) | Valid values are `RavenDB`, `SQLServer`, and `PostgreSQL`. To choose where message bodies are stored with SQL Server or PostgreSQL storage, use [`ServiceControl.Audit/MessageBody/StorageType`](/servicecontrol/audit-instances/configuration.md#message-body-storage-servicecontrol-auditmessagebodystoragetype). ### ServiceControl.Audit/Database/ConnectionString _Added in version 7.0.0_ The connection string for the SQL Server or PostgreSQL database. The database must already exist. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_AUDIT_DATABASE_CONNECTIONSTRING` | | **App config key** | `ServiceControl.Audit/Database/ConnectionString` | | **SCMU field** | `DATABASE CONNECTION STRING` | | Type | Default value | | --- | --- | | string | None (required for SQL Server and PostgreSQL storage) | ### ServiceControl.Audit/Database/Schema _Added in version 7.0.0_ The schema that contains the Audit instance tables. If the schema does not exist, setup creates it, which requires permission to create schemas. Changing this setting does not move existing data; an instance that already has data must keep using the schema that contains it. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_AUDIT_DATABASE_SCHEMA` | | **App config key** | `ServiceControl.Audit/Database/Schema` | | **SCMU field** | N/A | | Type | Default value | | --- | --- | | string | `sc_audit` | ### ServiceControl.Audit/AuditRetentionPeriod The grace period to keep an audit message before it is deleted. | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_AUDIT_AUDITRETENTIONPERIOD` | | **App config key** | `ServiceControl.Audit/AuditRetentionPeriod` | | **SCMU field** | `AUDIT RETENTION PERIOD` | | Type | Default value | | --- | --- | | timespan | None (required) | Valid range for this setting is from 1 hour to 365 days. > [!NOTE] > With PostgreSQL storage, the retention period must be between 1 and 90 days. ServiceControl does not start with a period outside that range. SQL Server storage uses the general range above. PostgreSQL-backed Audit instances are partitioned by day. The instance creates and drops daily partitions automatically, which requires the runtime role to own the Audit tables; see [database permissions](#database-permissions). ### ServiceControl.Audit/MaxBodySizeToStore A size limit, in bytes, for message bodies. Its effect depends on the storage type: | Storage | Effect | | --- | --- | | RavenDB | The largest message body that the Audit instance stores. Larger bodies cannot be viewed in ServicePulse. | | SQL Server or PostgreSQL | The largest text message body that is stored inline in the database. Larger text bodies, and bodies that are not text, are stored in the [external message body store](/servicecontrol/storage/external-body/index.md) and can be viewed in ServicePulse. A larger text body also keeps its first `MaxBodySizeToStore` bytes in the database for full-text search. See [what is stored where](/servicecontrol/storage/external-body/index.md#inline-storage-and-compression). | | Context | Name | | --- | --- | | **Environment variable** | `SERVICECONTROL_AUDIT_MAXBODYSIZETOSTORE` | | **App config key** | `ServiceControl.Audit/MaxBodySizeToStore` | | **SCMU field** | N/A | | Type | Default value | | --- | --- | | int | `102400` (100 KB) | > [!NOTE] > SQL Server and PostgreSQL storage do not support maintenance mode. An instance with `MaintenanceMode` set to `true` does not start. > [!WARNING] > Connect to PostgreSQL directly or through a connection pooler in session mode. A pooler in transaction mode, such as PgBouncer with `pool_mode = transaction`, breaks the advisory lock that the retention process holds, and retention stops running. Example environment variables for an Audit instance using file system body storage: ```text SERVICECONTROL_AUDIT_STORAGETYPE=PostgreSQL SERVICECONTROL_AUDIT_DATABASE_CONNECTIONSTRING="Host=postgres;Database=ServiceControl.Audit;Username=sc;Password=..." SERVICECONTROL_AUDIT_MESSAGEBODY_STORAGETYPE=FileSystem SERVICECONTROL_AUDIT_MESSAGEBODY_FILESYSTEM_STORAGEPATH=/var/servicecontrol/audit-bodies SERVICECONTROL_AUDIT_AUDITRETENTIONPERIOD=30 ``` See the [Audit instance settings reference](/servicecontrol/audit-instances/configuration.md#sql-server-and-postgresql-storage) for the complete reference. ## Run setup > [!IMPORTANT] > Run the instance setup process (`--setup`) before first use and after every upgrade. Setup applies the database migrations; the instance does not apply them at startup. Setup applies database migrations for both Error and Audit instances. Setup also provisions the external message body store. See [database permissions](#database-permissions) for the rights setup and normal operation need. See the [Error container setup](/servicecontrol/servicecontrol-instances/deployment/containers.md#initial-setup) or [Audit container setup](/servicecontrol/audit-instances/deployment/containers.md#initial-setup) instructions when deploying in containers. The ServiceControl Management Utility (SCMU) and the PowerShell module run setup when they create or upgrade an instance, and can create PostgreSQL-backed Error and Audit instances: - **SCMU:** choose `PostgreSQL` as the **STORAGE TYPE**, enter the **DATABASE CONNECTION STRING**, and check the **MESSAGE BODY PATH**, which defaults to `%ProgramData%\Particular\ServiceControl\\MessageBodies`. SCMU stores message bodies only on the file system. - **PowerShell:** pass `-StorageType PostgreSQL` and `-DatabaseConnectionString` to `New-ServiceControlInstance` or `New-ServiceControlAuditInstance`, and leave out `-DBPath` and `-DatabaseMaintenancePort`, which apply only to RavenDB. Also pass `-MessageBodyPath`, or choose [Azure Blob Storage](/servicecontrol/storage/external-body/azure-blob.md) or [Amazon S3](/servicecontrol/storage/external-body/amazon-s3.md) with `-MessageBodyStorageType`. See the [Error instance](/servicecontrol/servicecontrol-instances/deployment/powershell.md) and [Audit instance](/servicecontrol/audit-instances/deployment/powershell.md) PowerShell guides. To change the message body store after the instance is created, change the message body settings in the instance's configuration file. Bodies already stored are not moved, so make the change before the instance ingests messages. Neither tool sets `Database/Schema`, so instances they create use the default schema. Two Audit instances in one database both use the `sc_audit` schema, and two Error instances both use the current schema from `search_path`, usually `public`, so they share tables. Create independent instances of the same type in separate databases. To share a database between Audit instances on purpose, see [Scaling out audit ingestion](/servicecontrol/audit-instances/scaling-out-ingestion.md#sql-storage-shared-database); those instances must also share a message body store. An Error instance and an Audit instance can use one database, because their default schemas differ. ## Verify the setup Setup logs `PostgreSQL database migration completed` for an Error instance, or `Audit database migration completed` for an Audit instance, and exits without an error. After the instance starts, check that no storage [custom checks](/servicecontrol/servicecontrol-instances/index.md#notifications-health-monitoring) report a failure in ServicePulse. ## Troubleshooting - **Setup fails with a permission error:** grant the rights in [database permissions](#database-permissions), or create the schema in advance. - **Setup fails because "A primary instance and an audit instance cannot share a schema":** set a different `Database/Schema` for one of the instances. See [sharing a database](/servicecontrol/storage/index.md#sharing-a-database). - **The instance does not start with "Maintenance mode is not supported":** set `MaintenanceMode` to `false`. Maintenance mode is only available with embedded RavenDB storage. To perform database maintenance, stop the ServiceControl instance and access PostgreSQL directly. - **The ServiceControl Retention custom check fails:** check the instance log. A body store that refuses deletes stops failed-message retention; see [external message body storage operations](/servicecontrol/storage/external-body/index.md#operations). - **The Audit partition provisioning custom check fails:** make sure the Audit instance runs as the role that owns its tables. See [database permissions](#database-permissions). - **Connections fail with "Could not get a database access token":** check the inner exception for the credential's error. For Microsoft Entra ID, set `AZURE_TOKEN_CREDENTIALS` to the credential the instance should use, so that the error comes from that credential; see [Microsoft Entra ID](#authentication-token-authentication-microsoft-entra-id). For AWS IAM, check that the identity has the `rds-db:connect` permission for the database user and that the region is set. - **Messages show without their bodies after a restore:** restore the database and the external message body store from the same point in time. See [backup and restore](/servicecontrol/storage/index.md#backup-and-restore). ## Next steps - [Choose and configure an external message body store](/servicecontrol/storage/external-body/index.md) for each instance. - Plan [backup and restore](/servicecontrol/storage/index.md#backup-and-restore) of the database and the message bodies together; RavenDB-specific procedures do not apply. - To deploy in containers, see the [Error instance](/servicecontrol/servicecontrol-instances/deployment/containers.md) and [Audit instance](/servicecontrol/audit-instances/deployment/containers.md) container guides.