The target-postgres loader loads extracted data into a PostgreSQL database.

Alternative variants

Multiple variants of target-postgres are available. This document describes the transferwise variant, which was originally built to be used with PipelineWise.

Alternative options are datamill-co (default) and meltano.

Getting Started

Prerequisites

If you haven’t already, follow the initial steps of the Getting Started guide:

  1. Install Meltano
  2. Create your Meltano project
  3. Add an extractor to pull data from a source

Installation and configuration

Using the Command Line Interface

  1. Add the transferwise variant of the target-postgres loader to your project using meltano add:

     meltano add loader target-postgres --variant transferwise
    
  2. Configure the settings below using meltano config.

Using Meltano UI

  1. Start Meltano UI using meltano ui:

     meltano ui
    
  2. Open the Loaders interface at http://localhost:5000/loaders.
  3. Click the arrow next to the “Add to project” button for “PostgreSQL”.
  4. Choose “Add variant ‘transferwise’”.
  5. Configure the settings below in the “Configuration” interface that opens automatically.

Next steps

Follow the remaining step of the Getting Started guide:

  1. Run a data integration (EL) pipeline

If you run into any issues, refer to the “Troubleshooting” section below or learn how to get help.

Settings

target-postgres requires the configuration of the following settings:

These and other supported settings are documented below. To quickly find the setting you’re looking for, use the Table of Contents in the sidebar.

Minimal configuration

A minimal configuration of target-postgres in your meltano.yml project file will look like this:

plugins:
  loaders:
  - name: target-postgres
    variant: transferwise
    config:
      host: postgres.example.com
      port: 5432
      user: my_user
      dbname: my_database
      # default_target_schema: my_schema   # override if default (see below) is not appropriate

Sensitive values are most appropriately stored in the environment or your project’s .env file:

export TARGET_POSTGRES_PASSWORD=my_password

Host

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set host <host>

export TARGET_POSTGRES_HOST=<host>

Port

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set port 5502

export TARGET_POSTGRES_PORT=5502

User

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set user <user>

export TARGET_POSTGRES_USER=<user>

Password

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set password <password>

export TARGET_POSTGRES_PASSWORD=<password>

DBname

PostgreSQL database name

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set dbname <dbname>

export TARGET_POSTGRES_DBNAME=<dbname>

SSL

Using SSL via postgres sslmode='require' option.

If the server does not accept SSL connections or the client certificate is not recognized the connection will fail.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set ssl true

export TARGET_POSTGRES_SSL=true

Default Target Schema

  • Name: default_target_schema
  • Environment variable: TARGET_POSTGRES_DEFAULT_TARGET_SCHEMA, alias: TARGET_POSTGRES_SCHEMA, PG_SCHEMA
  • Default: $MELTANO_EXTRACT__LOAD_SCHEMA, which will expand to the value of the load_schema extra for the extractor used in the pipeline, which defaults to the extractor’s namespace, e.g. tap_gitlab for tap-gitlab.

Name of the schema where the tables will be created. If schema_mapping is not defined then every stream sent by the tap is loaded into this schema.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set default_target_schema <schema>

export TARGET_POSTGRES_DEFAULT_TARGET_SCHEMA=<schema>

Batch Size Rows

Maximum number of rows in each batch. At the end of each batch, the rows in the batch are loaded into Postgres.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set batch_size_rows 1000

export TARGET_POSTGRES_BATCH_SIZE_ROWS=1000

Flush All Streams

Flush and load every stream into Postgres when one batch is full. Warning: This may trigger the COPY command to use files with low number of records.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set flush_all_streams true

export TARGET_POSTGRES_FLUSH_ALL_STREAMS=true

Parallelism

The number of threads used to flush tables. 0 will create a thread for each stream, up to parallelism_max. -1 will create a thread for each CPU core. Any other positive number will create that number of threads, up to parallelism_max.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set parallelism -1

export TARGET_POSTGRES_PARALLELISM=-1

Parallelism Max

Max number of parallel threads to use when flushing tables.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set parallelism_max 8

export TARGET_POSTGRES_PARALLELISM_MAX=8

Default Target Schema Select Permission

  • Name: default_target_schema_select_permission
  • Environment variable: TARGET_POSTGRES_DEFAULT_TARGET_SCHEMA_SELECT_PERMISSION

Grant USAGE privilege on newly created schemas and grant SELECT privilege on newly created tables to a specific role or a list of roles. If schema_mapping is not defined then every stream sent by the tap is granted accordingly.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-snowflake set default_target_schema_select_permission <roles>

export TARGET_POSTGRES_DEFAULT_TARGET_SCHEMA_SELECT_PERMISSION=<roles>

Schema Mapping

Useful if you want to load multiple streams from one tap to multiple Postgres schemas.

If the tap sends the stream_id in <schema_name>-<table_name> format then this option overwrites the default_target_schema value.

Note, that using schema_mapping you can overwrite the default_target_schema_select_permission value to grant SELECT permissions to different groups per schemas or optionally you can create indices automatically for the replicated tables.

This setting can hold an object mapping source schema names to objects with target_schema and (optionally) target_schema_select_permissions keys.

How to use

Manage this setting directly in your meltano.yml project file:

plugins:
  loaders:
  - name: target-postgres
    variant: transferwise
    config:
      schema_mapping:
        <source_schema>:
          target_schema: <target_schema>
          target_schema_select_permissions: [<role1>, <role2>] # Optional
        # ...

        # For example:
        public:
          target_schema: repl_pg_public
          target_schema_select_permissions: [grp_stats]

Alternatively, manage this setting using meltano config or an environment variable:

meltano config target-postgres set schema_mapping <source_schema> target_schema <target_schema>
meltano config target-postgres set schema_mapping <source_schema> target_schema_select_permissions '["<role>", ...]'

export TARGET_POSTGRES_SCHEMA_MAPPING='{"<source_schema>": {"target_schema": "<target_schema>", ...}, ...}'

# Once a schema mapping has been set in `meltano.yml`, environment variables can be used
# to override specific nested properties:
export TARGET_POSTGRES_SCHEMA_MAPPING_<SOURCE_SCHEMA>_TARGET_SCHEMA=<target_schema>
export TARGET_POSTGRES_SCHEMA_MAPPING_<SOURCE_SCHEMA>_TARGET_SCHEMA_SELECT_PERMISSIONS='["<role>", ...]'

# For example:
meltano config target-postgres set schema_mapping public target_schema repl_pg_public
meltano config target-postgres set schema_mapping public target_schema_select_permissions '["grp_stats"]'

export TARGET_POSTGRES_SCHEMA_MAPPING_PUBLIC_TARGET_SCHEMA=new_repl_pg_public

Add Metadata Columns

  • Name: add_metadata_columns
  • Environment variable: TARGET_POSTGRES_ADD_METADATA_COLUMNS
  • Default: false

Metadata columns add extra row level information about data ingestions, (i.e. when was the row read in source, when was inserted or deleted in postgres etc.) Metadata columns are creating automatically by adding extra columns to the tables with a column prefix _SDC_. The column names are following the stitch naming conventions documented at https://www.stitchdata.com/docs/data-structure/integration-schemas#sdc-columns. Enabling metadata columns will flag the deleted rows by setting the _SDC_DELETED_AT metadata column. Without the add_metadata_columns option the deleted rows from singer taps will not be recongisable in Postgres.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set add_metadata_columns true

export TARGET_POSTGRES_ADD_METADATA_COLUMNS=true

Hard Delete

When hard_delete option is true then DELETE SQL commands will be performed in Postgres to delete rows in tables. It’s achieved by continuously checking the _SDC_DELETED_AT metadata column sent by the singer tap. Due to deleting rows requires metadata columns, hard_delete option automatically enables the add_metadata_columns option as well.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set hard_delete true

export TARGET_POSTGRES_HARD_DELETE=true

Data Flattening Max Level

  • Name: data_flattening_max_level
  • Environment variable: TARGET_POSTGRES_DATA_FLATTENING_MAX_LEVEL
  • Default: 0

Object type RECORD items from taps can be transformed to flattened columns by creating columns automatically. When value is 0 (default) then flattening functionality is turned off.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set data_flattening_max_level 2

export TARGET_POSTGRES_DATA_FLATTENING_MAX_LEVEL=2

Primary Key Required

  • Name: primary_key_required
  • Environment variable: TARGET_POSTGRES_PRIMARY_KEY_REQUIRED
  • Default: true

Log based and Incremental replications on tables with no Primary Key cause duplicates when merging UPDATE events. When set to true, stop loading data if no Primary Key is defined.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set primary_key_required false

export TARGET_POSTGRES_PRIMARY_KEY_REQUIRED=false

Validate Records

Validate every single record message to the corresponding JSON schema. This option is disabled by default and invalid RECORD messages will fail only at load time by Postgres. Enabling this option will detect invalid records earlier but could cause performance degradation.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set validate_records true

export TARGET_POSTGRES_VALIDATE_RECORDS=true

Temp Dir

Directory of temporary CSV files with RECORD messages.

How to use

Manage this setting using Meltano UI, meltano config, or an environment variable:

meltano config target-postgres set temp_dir /tmp/dir

export TARGET_POSTGRES_TEMP_DIR=/tmp/dir

Troubleshooting

Error: ld: library not found for -lssl or clang: error: linker command failed with exit code 1 or error: command 'clang' failed with exit status 1

This error message indicates that there is a problem installing OpenSSL. This Stack Overflow answer has specific recommendations on setting the LDFLAGS and/or CPPFLAGS environment variables. Set those prior to running meltano add loader target-postgres --variant transferwise.