> ## Documentation Index
> Fetch the complete documentation index at: https://docs.usebracket.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connecting Postgres

> Follow these steps to sync an existing Postgres table

## Creating your connection URI

1. Gather the following details for a user on your Postgres database:

   1. username (often `postgres`)
   2. password
   3. host
   4. port
   5. database
   6. schema
   7. table name

<Tip>If you are starting with a full connection URI string, it should follow the format below:</Tip>

```shell Postgres Connection URI theme={null}
postgresql://[user[:password]@][host][:port][,...][/database][?param1=value1&...]
```

2. Enter the details above to the web app at app.usebracket.com

<Accordion icon={"lightbulb"} title="Optional: Connecting via SSH">
  In the case that your postgres database is not accessible to the public, bracket can
  connect to your database through a bastion host/jump box, using SSH tunneling to
  connect. Follow these steps to ensure a successful connection:

  1. Click on the "Create SSH tunnel" button on the postgres connection details page,
     after you've entered the primary details for your postgres.

  <img src="https://mintcdn.com/bracket/bx6DzcifAPUE0Au7/img/ssh-tunnel-button.png?fit=max&auto=format&n=bx6DzcifAPUE0Au7&q=85&s=92a2d870b6f60e0013baa6e6238d7da6" width="662" height="198" data-path="img/ssh-tunnel-button.png" />

  2. Enter in the jump box and destination host connection details.
  3. You will then be provided with an ed25519 key pair. You can either copy the text or
     download the key. Keep in mind that this key is unique to this connection and will not be
     shown again. Once you've saved this information, you can close the modal window by clicking
     "Done".
  4. Enter that public key to your `~/.ssh/authorized_keys` file in your jump box. We recommend
     adding a comment like "bracket" after the key to keep your keys organized.
  5. After saving the `~/.ssh/authorized_keys` file with the , click "Next" on the bracket
     postgres connection details and bracket will notify you if the connection was successful!
</Accordion>

## Assigning user permissions

For Bracket to be able to be able to generate Postgres tables from other primary sources, we require the following permissions, which can be assigned via SQL.

```shell Database-level permissions theme={null}
GRANT CONNECT, CREATE ON DATABASE [your_database] TO [bracket_username];
```

```shell Schema-level permissions theme={null}
GRANT USAGE, CREATE, ALTER, REFERENCES ON SCHEMA [your_schema] TO [bracket_username];
```

For Bracket to be able to sync with an existing Postgres table, we require the following permissions:

```shell Schema-level permissions theme={null}
GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES, TRIGGER ON [your_table] TO [bracket_username];
```

## Optional: Creating a read only user on your database

<Note>Perform this step if you would like to give Bracket access through a dedicated account. Skip this step if you prefer to use an existing Postgres account during setup.</Note>

1. Create a new user and password

```shell Postgres User Creation theme={null}
CREATE USER bracket WITH PASSWORD 'any_safe_password';
```

2. Allow this new user to connect to the database

```shell Postgres Connection Access theme={null}
GRANT CONNECT ON DATABASE database_name TO bracket;
```

3. Allow Usage on the schema (typically 'public')

```shell Postgres Connection Access theme={null}
GRANT USAGE ON SCHEMA schema_name TO bracket;
```

4. Allow read access on a specific table

```shell Postgres Connection Access theme={null}
GRANT SELECT ON table_name TO bracket;
```
