Skip to main content

Connecting PostgreSQL to Salesforce


Once a PostgreSQL database has been created and configured to accept incoming connections, connecting an Apache Airflow environment to it is relatively straightforward. Before this connection can be established, however, the PostgreSQL provider must first be installed in the Airflow environment.

Installing the PostgreSQL provider in the Airflow environment

A provider is a package that can be installed in an Airflow environment to extend its capabilities.

To install the PostgreSQL provider, the following line must be added to the requirements file of the environment:

apache-airflow-providers-postgres>=6.3.0

The version of the package can be specified by appending it to the package name. Although this is not mandatory, it is highly recommended in order to ensure compatibility between the Airflow environment and the provider, while preventing unexpected changes after future releases.

The version can be strictly enforced by using the double equal sign (==). However, it is generally recommended to specify only the minimum acceptable version by using the greater than or equal sign (>=). This allows newer compatible versions to be installed automatically while guaranteeing that the required features are available.

The version of the PostgreSQL provider that can be installed depends on the version of Apache Airflow running in the environment.

Editing the requirements file

As a reminder, the requirements file is located in the S3 bucket associated with the MWAA environment. For the local runner, it is located in the requirements folder at the root of the aws-mwaa-local-runner directory.

Updating the Airflow environment

Once the requirements file has been updated, the Airflow environment must be updated so that the provider is installed.

The process to update an MWAA environment is described in the Setting up Apache Airflow documentation. The same page also explains how to update the local runner.

When updating an MWAA environment, if the requirements configuration is pinned to a specific version of the requirements file stored in Amazon S3, the newly uploaded version must be explicitly selected. Otherwise, the environment will continue using the previous version of the file.

Configuring the connection

After the PostgreSQL provider has been installed and the Airflow environment updated, the last step is to create a new connection.

Connections are managed from the Connections page, which can be accessed from the Admin section of the Airflow user interface.

In Airflow 3.0.6 and above, the navigation menu is located on the left. In Airflow 2.10 and below, it is located at the top of the page.

A new connection can be created by clicking Add Connection (Airflow 3.x) or the + button (Airflow 2.x).

The first two fields that must be completed are the Connection ID and the Connection Type.

  • Connection ID is simply the name of the connection. It can be any unique identifier. If there is only one PostgreSQL database in the environment, it is recommended to use postgres_default, which is the default value of the postgres_conn_id parameter used by the PostgreSQL Hook.

  • Connection Type must be set to Postgres. If this option does not appear in the list of available connection types, the PostgreSQL provider was most likely not installed successfully.

Unlike the Salesforce provider, the PostgreSQL provider does not rely on OAuth or certificates. Instead, it establishes a direct connection to the database using the connection parameters supplied in the Airflow connection.

The following fields should be completed.

Host

The Host field specifies the hostname or IP address of the PostgreSQL server.

If the database is hosted on Amazon RDS, this value corresponds to the RDS endpoint. For a locally hosted database, it is usually the hostname or IP address of the machine running PostgreSQL.

The Airflow environment must be able to reach this host over the network. For MWAA environments, this typically requires that both the Airflow environment and the database reside within the same VPC or within networks that can communicate with each other through appropriate routing and security rules.

Database

The Database field specifies the name of the PostgreSQL database that the connection should open.

A PostgreSQL server can host multiple databases simultaneously. This field identifies which one Airflow should connect to after authentication succeeds.

Login

The Login field contains the username used to authenticate with PostgreSQL.

For security reasons, it is recommended to create a dedicated database user for Apache Airflow instead of using the database administrator account. This user should be granted only the permissions required by the DAGs that will access the database.

Following the principle of least privilege reduces the impact of accidental modifications and limits the consequences of compromised credentials.

Password

The Password field contains the password associated with the PostgreSQL user.

Since this field contains sensitive information, Airflow stores it securely and does not display its value when the connection is edited.

Port

The Port field specifies the TCP port used by the PostgreSQL server.

The default PostgreSQL port is 5432, which should be used unless the server has been configured to listen on another port.

Extra

Most PostgreSQL connections do not require any additional configuration.

The Extra field can be used to specify optional connection parameters in JSON format. Examples include SSL configuration, connection options or additional parameters supported by the PostgreSQL driver.

For example, if SSL encryption is required by the PostgreSQL server, the following configuration can be used:

{
  "sslmode": "require"
}

The exact parameters that should be specified depend on the configuration of the PostgreSQL server.

Using the PostgreSQL connection in a DAG

The PostgreSQL provider includes the PostgresHook, which retrieves the configured connection from Airflow and establishes the connection to the database.

from datetime import datetime

from airflow.decorators import dag, task
from airflow.providers.postgres.hooks.postgres import PostgresHook


@dag(
    dag_id="postgres_dag_example",
    schedule=None,
    start_date=datetime(2026, 1, 1),
    catchup=False,
)
def postgres_dag_example():

    @task
    def postgres_task_example():

        # By default, the PostgresHook retrieves the connection
        # named "postgres_default". If another connection ID was
        # used, it must be specified here.

        hook = PostgresHook()
        # or
        # hook = PostgresHook(postgres_conn_id="my_postgres_connection")

        # Execute a SQL query.

        records = hook.get_records(
            "SELECT id, first_name, last_name FROM employees LIMIT 5"
        )

        for record in records:
            print(record)

    postgres_task_example()


postgres_dag_example()

If more advanced database operations are required, the PostgresHook also provides direct access to the underlying database connection and cursor, allowing any SQL statement supported by PostgreSQL to be executed.