Loading

Elastic PostgreSQL connector reference

The Elastic PostgreSQL connector is a connector for PostgreSQL. This connector is written in Python using the Elastic connector framework.

This connector uses the generic database connector source code (branch main, compatible with Elastic 9.0). View the specific source code for this connector (branch main, compatible with Elastic 9.0).

Important

As of Elastic 9.0, managed connectors on Elastic Cloud Hosted are no longer available. All connectors must be self-managed.

This connector is available as a self-managed connector. To use this connector, satisfy all self-managed connector requirements.

To create a new PostgreSQL connector:

  1. In the Kibana UI, navigate to the Search → Content → Connectors page from the main menu, or use the global search field.
  2. Follow the instructions to create a new PostgreSQL self-managed connector.

You can use the Elasticsearch Create connector API to create a new self-managed PostgreSQL self-managed connector.

For example:

 PUT _connector/my-postgresql-connector {
  "index_name": "my-elasticsearch-index",
  "name": "Content synced from PostgreSQL",
  "service_type": "postgresql"
}

Refer to the Elasticsearch API documentation for details of all available Connector APIs.

To use this connector as a self-managed connector, see Self-managed connectors.

Tip

Users must set track_commit_timestamp to on. To do this, run ALTER SYSTEM SET track_commit_timestamp = on; in PostgreSQL server.

For additional operations, see.

Note

For an end-to-end example of the self-managed connector workflow, see Tutorial.

PostgreSQL versions 11 to 15 are compatible with Elastic connector frameworks.

Set the following configuration fields:

host

The server host address where the PostgreSQL instance is hosted. Examples:

  • 192.158.1.38
  • demo.instance.demo-region.demo.service.com
port

The port where the PostgreSQL instance is hosted. Examples:

  • 5432
  • 9090
username
The username of the PostgreSQL account.
password
The password of the PostgreSQL account.
database

Name of the PostgreSQL database. Examples:

  • employee_database
  • customer_database
schema
The schema of the PostgreSQL database.
tables

A list of tables separated by commas. The PostgreSQL connector will fetch data from all tables present in the configured database, if the value is * . Default value is *. Examples:

  • table_1, table_2
  • *

This field can be bypassed when using advanced sync rules.

ssl_enabled
Whether SSL verification will be enabled. Default value is True.
ssl_ca

Content of SSL certificate (if SSL is enabled). If SSL is disabled, the ssl_ca value will be ignored.

You can deploy the PostgreSQL connector as a self-managed connector using Docker. Follow these instructions.

Refer to DOCKER.md in the elastic/connectors repo for more details.

Find all available Docker images in the official registry.

Tip

We also have a quickstart self-managed option using Docker Compose, so you can spin up all required services at once: Elasticsearch, Kibana, and the connectors service. Refer to this README in the elastic/connectors repo for more information.

  • Tables must be owned by a PostgreSQL user.
  • Tables with no primary key defined are skipped.
  • To fetch the last updated time in PostgreSQL, track_commit_timestamp must be set to on. Otherwise, all data will be indexed in every sync.
Note
  • Files bigger than 10 MB won’t be extracted.
  • Permissions are not synced. All documents indexed to an Elastic deployment will be visible to all users with access to that Elastic Deployment.
Note

A //connectors-sync-types-full, full sync is required for advanced sync rules to take effect.

Advanced sync rules are defined through a source-specific DSL JSON snippet.

Here is some example data that will be used in the following examples.


employee table

emp_id name age
3 John 28
10 Jane 35
14 Alex 22


customer table

c_id name age
2 Elm 24
6 Pine 30
9 Oak 34


Multiple table queries

[
  {
    "tables": [
      "employee"
    ],
    "query": "SELECT * FROM employee"
  },
  {
    "tables": [
      "customer"
    ],
    "query": "SELECT * FROM customer"
  }
]


Multiple table queries with id_columns

In 8.15.0, we added a new optional id_columns field in our advanced sync rules for the PostgreSQL connector. Use the id_columns field to ingest tables which do not have a primary key. Include the names of unique fields so that the connector can use them to generate unique IDs for documents.

[
  {
    "tables": [
      "employee"
    ],
    "query": "SELECT * FROM employee",
    "id_columns": ["emp_id"]
  },
  {
    "tables": [
      "customer"
    ],
    "query": "SELECT * FROM customer",
    "id_columns": ["c_id"]
  }
]

This example uses the id_columns field to specify the unique fields emp_id and c_id for the employee and customer tables, respectively.


Filtering data with WHERE clause

[
  {
    "tables": ["employee"],
    "query": "SELECT * FROM employee WHERE emp_id > 5"
  }
]


JOIN operations

[
  {
    "tables": ["employee", "customer"],
    "query": "SELECT * FROM employee INNER JOIN customer ON employee.emp_id = customer.c_id"
  }
]
Warning

When using advanced rules, a query can bypass the configuration field tables. This will happen if the query specifies a table that doesn’t appear in the configuration. This can also happen if the configuration specifies * to fetch all tables while the advanced sync rule requests for only a subset of tables.

The connector framework enables operators to run functional tests against a real data source. Refer to Connector testing for more details.

To perform E2E testing for the PostgreSQL connector, run the following command:

$ make ftest NAME=postgresql

For faster tests, add the DATA_SIZE=small flag:

make ftest NAME=postgresql DATA_SIZE=small

There are no known issues for this connector. Refer to Known issues for a list of known issues for all connectors.

See Troubleshooting.

See Security.