Tap PostgreSQL

PostgreSQL setup requirements

(Section based on Stitch documentation)

Step 1: Check if you have all the required credentials for replicating data from PostgreSQL

  • CREATEROLE or SUPERUSER privilege - Either permission is required to create a database user for PipelineWise.

Step 2. Create a PipelineWise database user

Next, you’ll create a dedicated database user for PipelineWise. Create a new user and grant the required permissions on the database, schema and tables that you want to replicate:

  • CREATE USER pipelinewise WITH ENCRYPTED PASSWORD '<password>'

  • GRANT CONNECT ON DATABASE <database_name> TO pipelinewise

  • GRANT USAGE ON SCHEMA <schema_name> TO pipelinewise

  • GRANT SELECT ON ALL TABLES IN SCHEMA <schema_name> TO pipelinewise

Step 3: Configure Log-based Incremental Replication

Note

This step is only required if you use Log Based replication method.

Warning

Log Based for PostgreSQL-based databases requires:

  • PostgreSQL databases running PostgreSQL versions 9.4.x or greater.

  • To avoid a critical PostgreSQL bug, use at least one of the following minor versions

    • PostgreSQL 11.2

    • PostgreSQL 10.7

    • PostgreSQL 9.6.12

    • PostgreSQL 9.5.16

    • PostgreSQL 9.4.21

  • A connection to the master instance. Log-based replication will only work by connecting to the master instance.

Step 3.1: Install the wal2json plugin

To use Log Based for your PostgreSQL integration, you must install the wal2json plugin. The wal2json plugin outputs JSON objects for logical decoding, which Stitch then uses to perform Log-based Replication.

Steps for installing the plugin vary depending on your operating system. Instructions for each operating system type are in the wal2json’s GitHub repository:

After you’ve installed the plugin, you can move onto the next step.

Step 3.2: Edit the database configuration file

Locate the database configuration file (usually postgresql.conf) and define the parameters as follows:

wal_level=logical
max_replication_slots=5
max_wal_senders=5

Note: For max_replication_slots and max_wal_senders, we’re defaulting to a value of 5. This should be sufficient unless you have a large number of read replicas connected to the master instance.

Step 3.3: Restart the PostgreSQL service

Restart your PostgreSQL service to ensure the changes take effect.

Step 3.4: Create a replication slot

Next, you’ll create a dedicated logical replication slot for Stitch. In PostgreSQL, a logical replication slot represents a stream of database changes that can then be replayed to a client in the order they were made on the original server. Each slot streams a sequence of changes from a single database.

Note: Replication slots are specific to a given database in a cluster. If you want to connect multiple databases - whether in one integration or several - you’ll need to create a replication slot for each database.

  1. Log into the master database as a superuser.

  2. Using the wal2json plugin, create a logical replication slot:

SELECT *
FROM pg_create_logical_replication_slot('pipelinewise_<database_name>', 'wal2json');

3. Log in as the PipelineWise user and verify you can read from the replication slot, replacing pipelinewise_<database_name> with the name of the replication slot:

SELECT COUNT(*)
FROM pg_logical_slot_peek_changes('pipelinewise_<database_name>', null, null);

Note: wal2json is required to use Log Based in Stitch for PostgreSQL-backed databases.

Configuring what to replicate

PipelineWise configures every tap with a common structured YAML file format. A sample YAML for Postgres replication can be generated into a project directory by following the steps in the Generating Sample Pipelines section.

Example YAML for tap-postgres:

---

# ------------------------------------------------------------------------------
# General Properties
# ------------------------------------------------------------------------------
id: "postgres_sample"                  # Unique identifier of the tap
name: "Sample Postgres Database"       # Name of the tap
type: "tap-postgres"                   # !! THIS SHOULD NOT CHANGE !!
owner: "somebody@foo.com"              # Data owner to contact


# ------------------------------------------------------------------------------
# Source (Tap) - PostgreSQL connection details
# ------------------------------------------------------------------------------
db_conn:
  host: "<HOST>"                       # PostgreSQL host
  port: 5432                           # PostgreSQL port
  user: "<USER>"                       # PostfreSQL user
  password: "<PASSWORD>"               # Plain string or vault encrypted
  dbname: "<DB_NAME>"                  # PostgreSQL database name
  #filter_schemas: "schema1,schema2"   # Optional: Scan only the required schemas
                                       #           to improve the performance of
                                       #           data extraction


# ------------------------------------------------------------------------------
# Destination (Target) - Target properties
# Connection details should be in the relevant target YAML file
# ------------------------------------------------------------------------------
target: "snowflake"                    # ID of the target connector where the data will be loaded
batch_size_rows: 20000                 # Batch size for the stream to optimise load performance


# ------------------------------------------------------------------------------
# Source to target Schema mapping
# ------------------------------------------------------------------------------
schemas:

  - source_schema: "public"            # Source schema in postgres with tables
    target_schema: "repl_pg_public"    # Target schema in the destination Data Warehouse
    target_schema_select_permissions:  # Optional: Grant SELECT on schema and tables that created
      - grp_stats

    # List of tables to replicate from Postgres to destination Data Warehouse
    #
    # Please check the Replication Strategies section in the documentation to understand the differences.
    # For LOG_BASED replication method you might need to adjust the source mysql/ mariadb configuration.
    tables:
      - table_name: "table_one"
        replication_method: "INCREMENTAL"   # One of INCREMENTAL, LOG_BASED and FULL_TABLE
        replication_key: "last_update"      # Important: Incremental load always needs replication key

        # OPTIONAL: Load time transformations
        #transformations:
        #  - column: "last_name"            # Column to transform
        #    type: "SET-NULL"               # Transformation type

      # You can add as many tables as you need...
      - table_name: "table_two"
        replication_method: "LOG_BASED"     # Important! Log based must be enabled in MySQL

  # You can add as many schemas as you need...
  # Uncomment this if you want replicate tables from multiple schemas
  #- source_schema: "another_schema_in_postgres"
  #  target_schema: "another