Skip to main content

PostgreSQL

Databases Analytics

Synopsis​

Creates a PostgreSQL target that writes records into one table of an existing PostgreSQL database. Each field of a record fills the column with the same name, or the whole record goes into a single JSON column.

Schema​

- name: <string>
description: <string>
type: postgres
pipelines: <pipeline[]>
status: <boolean>
properties:
address: <string>
port: <integer>
database: <string>
schema: <string>
table: <string>
username: <string>
password: <string>
json_column: <string>
batch_size: <integer>
max_bytes: <integer>
timeout: <integer>
field_format: <string>
tls:
status: <boolean>
verify: <boolean>
ca_name: <string>
cert_name: <string>
key_name: <string>
passphrase: <string>
server_name: <string>
min_tls_version: <string>
max_tls_version: <string>
debug:
status: <boolean>
dont_send_logs: <boolean>

Configuration​

The following fields are used to define the target:

FieldRequiredDefaultDescription
nameYTarget name
descriptionN-Optional description
typeYMust be postgres
pipelinesN-Optional post-processor pipelines
statusNtrueEnable/disable the target

Connection​

FieldRequiredDefaultDescription
addressY-Host name or IP address of the PostgreSQL server
portN5432Port of the PostgreSQL server
databaseY-The database that holds the table
usernameN-The user that writes to the table. It needs permission to insert into it
passwordN-Password for that user
timeoutN30Seconds one database call may take. At least 1

Table​

FieldRequiredDefaultDescription
schemaNpublicThe table's schema
tableY-The table to write to. It must already exist
json_columnN-The column that receives the whole record. A json, jsonb or text column. Leave it out to fill columns by name. See Columns by Name or Whole Record as JSON

The database, schema, table and JSON column names must start with a letter or an underscore, and use only letters, digits and underscores. Any other name is refused and the target does not start.

Delivery​

FieldRequiredDefaultDescription
batch_sizeN1000The most records in one write. The target's own value first, then the service-level batch_size, then 1000
max_bytesN16777216 (16 MiB)The most bytes of JSON in one write
field_formatN-Converts each record to a format such as ECS or OCSF before it is written. See applicable Normalization section
note

max_bytes caps the size of a single request body or batch. It behaves differently from max_size on the file-writing targets, and the difference is easy to get wrong:

  • An explicit max_bytes: 0 IS honoured and removes the ceiling on targets that have no ceiling of their own. The value is read with Int64, which returns what you wrote — unlike max_size, which is read with MustInt64 and silently substitutes its default for a zero. Where a target has a fixed ceiling of its own, its page says so and a zero is clamped to that ceiling instead.
  • A negative value is rejected at configuration time with max_bytes must not be negative.

The ceiling is validated when the target is built and enforced again on every batch, so a value the receiving service will refuse fails early rather than per request.

TLS​

FieldRequiredDefaultDescription
tls.statusNfalseConnect over TLS. TLS stays off unless this is true

TLS material is resolved through the shared client builder, so these keys mean the same thing on every target that uses it. They are nested under a tls: block.

FieldRequiredDefaultDescription
tls.verifyNtrueVerify the server certificate. On by default — set it to false only to accept an untrusted certificate, and only where you control the network path
tls.server_nameN-SNI hostname override for the handshake. Use it when the certificate's name does not match the address you connect to
tls.ca_nameN-CA bundle used to verify the server certificate. When unset the host trust store is used; when set it replaces the host trust store rather than adding to it
tls.cert_nameN*-Client certificate, for mutual TLS
tls.key_nameN*-Client private key, for mutual TLS
tls.passphraseN-Passphrase for an encrypted client private key
tls.min_tls_versionNtls1.2Lowest protocol version accepted (tls1.0, tls1.1, tls1.2, tls1.3)
tls.max_tls_versionNtls1.3Highest protocol version accepted (tls1.0, tls1.1, tls1.2, tls1.3)

* cert_name and key_name are individually optional but must be supplied together — a certificate without its key, or a key without its certificate, is a configuration error.

Scheduling​

See Scheduling and Pool Behavior for interval and cron fields shared by all targets.

Debug Options​

FieldRequiredDefaultDescription
debug.statusNfalseEnable debug logging
debug.dont_send_logsNfalseProcess logs but don't send to target (testing)

Details​

The PostgreSQL target writes records into one table that already exists. It never creates the table or changes it, and it only adds rows. Each batch is written in one go, and each worker uses its own connection to the database.

Columns by Name or Whole Record as JSON​

The target works in one of two modes:

  • Columns by name, the default: each top-level field of a record fills the column with the same name, with the same upper and lower case. A field with no matching column is skipped. A record that is not a JSON object, or that fills no column, is skipped and counted as dropped.
  • Whole record as JSON, when json_column is set: the whole record goes into that one column, and every other column keeps its default. A record that is not valid JSON is written as a JSON string.

Names match exactly, including case. PostgreSQL stores a name created without quotes in lowercase, so a column created as EventTime without quotes is eventtime, and a field named EventTime does not fill it.

The target leaves out generated columns, and identity columns that are always generated, because PostgreSQL refuses a value written to them.

warning

A field with no matching column is skipped without an error. If no field of a record matches any column, the record is dropped, and when a whole batch keeps nothing the Director logs a warning. Check the column names against the fields your pipeline emits before relying on the target.

Prerequisites​

  1. A running PostgreSQL server, reachable from the Director host
  2. The database and the table, already created
  3. A user with permission to insert into the table

Examples​

Basic​

Writing each record's fields into the columns of the same name:

targets:
- name: postgres_events
type: postgres
properties:
address: "postgres.example.com"
database: "analytics"
table: "events"
username: "datastream"
password: "secret"

port and schema are left out, so they default to 5432 and public.

Whole Record as JSON​

Writing each whole record into a jsonb column:

targets:
- name: postgres_raw
type: postgres
properties:
address: "postgres.example.com"
database: "analytics"
schema: "logs"
table: "raw_events"
json_column: "record"
username: "datastream"
password: "secret"

Every other column of logs.raw_events keeps its default, such as a timestamp column that defaults to the time of the insert.

With TLS​

Connecting over TLS to a server whose certificate comes from a private certificate authority:

targets:
- name: postgres_tls
type: postgres
properties:
address: "postgres.example.com"
database: "analytics"
table: "events"
username: "datastream"
password: "secret"
tls:
status: true
ca_name: "postgres-ca.pem"

Troubleshooting​

SituationResult
The database refuses a value, for example one that does not fit its columnThe batch is sent again one record at a time. Only the records that still fail are dropped, and they are counted as dropped
The connection or the server has a temporary problemThe batch is retried
A column was removed from the tableThe target reads the table's columns again once and retries the batch
The table does not exist when the target startsThe target stops with an error. Create the table first and give the user access to it

The table was not found​

postgres table analytics.public.events was not found, or it has no columns this target can write. Create the table first, and give the user access to it

Cause: the table named in schema and table does not exist, the user cannot see it, or every one of its columns is generated. The target reads the table's columns when it starts, so this shows up at startup rather than on the first batch.

Fix: create the table, or correct schema and table, and grant the user access to it. Remember that a name created without quotes is stored in lowercase.

Records are dropped although nothing fails​

Cause: the fields of the records match no column of the table. Names match exactly, including case.

Fix: rename the columns after the fields your pipeline emits, or set json_column to store the whole record in one column.

Configuration errors​

These stop the target from starting:

Reason textFix
address is required for postgres targetSet address
database is required for postgres targetSet database
table is required for postgres targetSet table
invalid database name "..." for postgres targetUse a name of letters, digits and underscores that starts with a letter or an underscore. The same rule applies to schema, table and json_column
timeout must be at least 1 second for postgres target, got ...Set timeout to 1 or more
batch_size must be a whole number greater than 0 for postgres target, got ...Use a whole number of at least 1, or leave batch_size out to use the service-level value
failed to build TLS config: ...The rest of the line names the TLS setting at fault