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

# Connect to PostgreSQL

This topic describes how to connect to [PostgreSQL](https://www.postgresql.org/) from Domino. You must have network connectivity between PostgreSQL and your Domino deployment.

The easiest way to connect to PostgreSQL from Domino is to create a Domino Data Source as described below.

## Create a Postgres Data Source

1. From the navigation pane, click **Data > Data Sources**.

2. Click **Create a Data Source**.

3. In the New Data Source window, from **Select Data Store**, select **PostgreSQL**.

4. Enter the unique storage account name in **Account Name**.

5. Enter the **Host**, **Port**, and **Database** name.

6. Enter the **Data Source Name**.

7. Optional: Enter a **Description** to explain the purpose of the Data Source to others.

8. Click **Next**.

9. Specify the credentials for authenticating to Postgres.

   Basic authentication is supported by default. IAM credential propagation might also be available if your administrator has [enabled it](/cloud/admin/data-administration/external-data/configure-ds-auth#iam-auth).

   <Note>
     IAM-authenticated connections can be used only for executing jobs and workspaces. Other execution types, such as scheduled jobs and Domino endpoints, require basic authentication.
   </Note>

10. Click **Next** (or **Skip for Now** to configure authentication later).

11. Enter the users and organizations who can view and use the Data Source in projects.

12. Click **Finish Setup**.

## Alternate way to connect to a Postgres Data Source

<Warning>
  This section describes an alternate method to connect to the Postgres Data Source. Domino does not officially support this method.
</Warning>

This instruction assumes you already have [pip](https://pypi.org/project/pip/) installed.

1. For Python and psycopg2, Domino recommends the [psycopg2](https://pypi.org/project/psycopg2/) library to interact with PostgreSQL databases from Python.

2. Use the following Dockerfile instruction to [install psycopg2](https://www.psycopg.org/docs/install.html) in your environment.

   ```dockerfile theme={null}
   RUN pip install psycopg2
   ```

3. Set the following as Domino environment variables to store secure information about your PostgreSQL connection.

   * `POSTGRES_HOST`

     Hostname where your DB is running. Make sure your PostgreSQL DB and network firewall are configured to accept connections from Domino.

   * `POSTGRES_USER`

     The PostgreSQL user you want to authenticate as.

   * `POSTGRES_PASSWORD`

     The password for the user chosen previously.

     See [Store Project credentials](/cloud/platform-capabilities/core-concepts/projects/configure-projects/store-project-credentials) to learn more about Domino environment variables.

4. See the [psycopg2 documentation](https://www.psycopg.org/docs/) for detailed information about how to use the package. The following is an example to connect to PostgreSQL with `psycopg2` where:

   * You have set up environment variables with the hostname, username, and password.

   * Your user has access to a database named `db1` in the target PostgreSQL instance.

   * The database contains a table named `metrics`.

     ```python theme={null}
     import psycopg2
     import os

     # fetch values from environment variables and set the target database
     hostname = os.environ['POSTGRES_HOST']
     username = os.environ['POSTGRES_USER']
     password = os.environ['POSTGRES_PASSWORD']
     dbname = 'db1'

     # set up a connection object with parameters for your database
     conn = psycopg2.connect(
       host=hostname,
       port=5432,
       user=username,
       password=password,
       database=dbname, )

     # create a cursor in your connection
     cur = conn.cursor()

     # execute a query on the metrics table and store the response
     cur.execute("SELECT * FROM metrics;")
     results = cur.fetchall()

     # display the contents of the response
     print(results)
     ```

     The results object created in the previous example is a Python array of entries from the queried table.

### R and RPostgreSQL

1. To connect to R and RPostgreSQL, Domino recommends the [RPostgreSQL](https://www.rdocumentation.org/packages/RPostgreSQL/versions/0.6-2) library to interact with PostgreSQL databases from R.

2. Use the following Dockerfile instruction to add RPostgreSQL to your environment.

   ```dockerfile theme={null}
   RUN R -e 'install.packages("RPostgreSQL")'
   ```

3. Set up the Domino environment variables to store secure information about your PostgreSQL connection.

   * `POSTGRES_HOST`

     Hostname where your DB is running. Make sure your PostgreSQL DB and network firewall are configured to accept connections from Domino.

   * `POSTGRES_USER`

     The PostgreSQL user you want to authenticate as.

   * `POSTGRES_PASSWORD`

     The password for the user chosen previously.

     See [Store Project credentials](/cloud/platform-capabilities/core-concepts/projects/configure-projects/store-project-credentials) to learn more about Domino environment variables.

4. See the [RPostgreSQL documentation](https://www.rdocumentation.org/packages/RPostgreSQL/versions/0.6-2) for information about how to use the package. The following is an example for connecting to PostgreSQL with RPostgreSQL where:

   * You have set up environment variables with the hostname, username, and password.

   * Your user has access to a database named `db1` in the target PostgreSQL instance.

   * The database contains a table named `` metrics` ``.

     ```r theme={null}
     # load the library
     library(RPostgreSQL)

     # fetch values from environment variables and set the target database
     hostname <- Sys.getenv['POSTGRES_HOST']
     username <-  Sys.getenv['POSTGRES_USER']
     password <- Sys.getenv['POSTGRES_PASSWORD']
     database <- 'db1'

     # set up a driver and use it to create a connection to your database
     drv <- dbDriver("PostgreSQL")
     conn <- dbConnect(
       drv,
       host=hostname,
       port=5432,
       user=username,
       password=password,
       dbname=database )

     # run a query and load the response into a dataframe
     df_postgres <- dbGetQuery(conn, "SELECT * from metrics;")

     # close your connection when finished
     dbDisconnect(conn)
     ```

## Next steps

* After connecting to your Data Source, learn how to [Use Data Sources](/cloud/platform-capabilities/core-concepts/data/data-source-connectors/use-data-sources).

* [Share this Data Source](/cloud/platform-capabilities/core-concepts/data/sharing-and-security/share-data-sources) with your collaborators.
