# PostgreSQL

## Authentication

To allow Formsort to write to PostgreSQL, create a new user with insert permissions to the schema that contains your data table. You can then add the credentials for the user directly into Formsort.

Do not reuse a user with more permissions than are necessary to INSERT rows - Formsort does not even need SELECT access, and should not have any administrative permissions. Note: do grant`USAGE` right on the sequence underpinning the primary key. ***Example*** using `formsort` role:

```sql
GRANT INSERT ON TABLE answer_set TO formsort;
GRANT USAGE ON SEQUENCE answer_set_id_seq TO formsort;
```

## Row Insert Frequency

The frequency of inserting a row to PostgreSQL can be configured as:

* *On Finalize:* only at the end of the flow.
* *On Savepoint:* after each step marked as *save point*, and at the end of the flow.
* *Every step:* at the end of each step (when the responder advances using the *Next* button), and at the end of the flow.
* *Debounced*: when the responder abandons the flow after a period of inactivity, and at the end of the flow. Formsort recommends using this setting to reduce the load.

## Security

Formsort backend system will exclusively connect to Postgres from the static IP `18.217.92.196`.

## Schema

Because the Formsort user should not have administrative permissions, you must create it yourself, using a SQL command.

Create a table named `answer_set` with the following command:

```sql
CREATE TABLE answer_set (
    id SERIAL PRIMARY KEY,
    submitted_at timestamp with time zone NOT NULL,
    responder_uuid uuid NOT NULL,
    flow_label character varying,
    variant_label character varying,
    variant_revision_uuid uuid,
    event_type character varying,
    answers jsonb,
    schema_version smallint
);
CREATE UNIQUE INDEX answer_set_set_id_idx ON answer_set(id);
```

{% hint style="info" %}
Formsort only lets you write to `answer_set` table.
{% endhint %}

## Adding multiple PostgreSQL instances&#x20;

There is an option to send answer payloads to multiple Postgres destinations. This is useful if you'd like to share your data across multiple endpoints, or have payloads sent at different [submission frequencies](https://docs.formsort.com/integrations/getting-data-out/submission-frequencies) go to different destinations.&#x20;

To enable multiple destinations, first complete setup for an initial destination (**Destination 1**). Then, click the "**+ Add destination**" button. Up to 3 instances can be added.&#x20;

![](https://1036686854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJPnL__mOdr_mLZ8nwf%2Fuploads%2FTOzapkUGxBfTx2ZvZMDx%2Fimage.png?alt=media\&token=30c3acc7-c33e-45bd-98ad-bc076b515bcb)

Once you've finished configuring all instances of your PostgreSQL integrations, be sure to **Save** your work with the button in the top right corner.&#x20;

{% hint style="info" %}
If you have deployed flows previous to integrating with or updating Postgres instances, it is advisable to re-[deploy](https://docs.formsort.com/core-concepts/versioning-in-formsort-deploying) those flows.&#x20;
{% endhint %}
