Follow these instructions to replicate your generic PostgreSQL database to your destination using Fivetran.
Prerequisiteslink
To connect your generic PostgreSQL database to Fivetran, you need:
- PostgreSQL version 8.4 - 14.x
- Your database host’s IP (e.g.,
1.2.3.4
) or domain (your.server.com
) - Your database’s port (usually
5432
) - TLS enabled on your database
Setup instructionslink
Choose incremental sync mechanismlink
To keep your data up to date after the initial sync, we use one of the following incremental sync methods:
- logical replication with the
pgoutput
plugin - logical replication with the
test_decoding
plugin - XMIN
- Fivetran Teleport Sync BETA
The first three methods keep a record of recent data changes, which allows Fivetran to update only the data that has changed since our last sync. Fivetran Teleport Sync instead takes snapshots of tables to calculate differences.
TIP: We recommend using logical replication as your incremental update mechanism because it is faster than XMIN replication and allows Fivetran to detect deleted rows for tables with primary keys. Learn more in our Updating data documentation.
Logical replication with the pgoutput
plugin
IMPORTANT: You can only enable logical replication with the
pgoutput
plugin if your PostgresSQL server version is 10+. Prior minor versions have breaking bugs.
Logical replication is based on logical decoding of the PostgreSQL write-ahead log (WAL). Fivetran reads the WAL using the pgoutput
plugin to detect any new or changed data. This plugin replicates from your custom publication without needing additional libraries.
To learn more, see our logical replication with the pgoutput
plugin documentation.
Logical replication with the test_decoding
plugin
IMPORTANT: You can only enable logical replication with the
test_decoding
plugin if your PostgreSQL server version is 9.4.15 or later. Prior minor versions have breaking bugs.
Logical replication is based on logical decoding of the PostgreSQL write-ahead log (WAL). Fivetran reads the WAL using the test_decoding
plugin to detect any new or changed data. This plugin receives WAL changes through the logical decoding mechanism and converts them into human-readable text.
To learn more, see our logical replication with the test_decoding
plugin documentation.
XMIN
The XMIN method is based on the hidden xmin
system column that is present in all PostgreSQL tables. With XMIN, Fivetran must scan every table in full to detect updated data. We do not recommend XMIN for near real-time data needs because XMIN replication is slower than logical replication and doesn’t allow Fivetran to detect deleted rows.
Learn more in our XMIN documentation.
Fivetran Teleport Sync BETA
Fivetran Teleport Sync is a proprietary database replication method that allows Fivetran to incrementally replicate your database with no additional setup other than a read-only SQL connection.
Learn more in our Fivetran Teleport Sync documentation.
Create userlink
Create a database user for Fivetran’s exclusive use.
-
Open a connection to your PostgreSQL database in a PostgreSQL console (such as a SQL workbench or psql).
-
Create a user for Fivetran by executing the following SQL command. Replace
<username>
andsome-password
with a username and password of your choice.
CREATE USER <username> PASSWORD 'some-password';
Grant read-only accesslink
Grant the Fivetran user read-only access to all tables by running the following commands. To grant access to a schema other than PostgreSQL’s default public
schema, replace public
with the schema name.
GRANT USAGE ON SCHEMA "public" TO <username>;
GRANT SELECT ON ALL TABLES IN SCHEMA "public" TO <username>;
ALTER DEFAULT PRIVILEGES IN SCHEMA "public" GRANT SELECT ON TABLES TO <username>;
NOTE: The last command makes sure that any future tables will be accessible to Fivetran.
If you want to grant access to multiple schemas, you must run these three commands for each schema.
Restrict access to tables (optional)
If you want to limit Fivetran’s access to your tables, grant the Fivetran user access to only the tables that you would like to sync. You must individually grant access for each table that you want to sync. You cannot grant access to all tables and then revoke access for a subset of tables.
-
Ensure that the Fivetran user has access to the schema that contains your table(s).
GRANT USAGE ON SCHEMA "some_schema" TO <username>;
-
Revoke any previously granted permission to all tables in that schema.
ALTER DEFAULT PRIVILEGES IN SCHEMA "some_schema" REVOKE SELECT ON TABLES FROM <username>; REVOKE SELECT ON ALL TABLES IN SCHEMA "some_schema" FROM <username>;
-
Repeat the following command for each table you want Fivetran to sync.
GRANT SELECT ON "some_schema"."some_table" TO <username>;
-
By default, any tables that you create in the future will be excluded from the Fivetran user’s access. To grant access to new tables, run the following command.
ALTER DEFAULT PRIVILEGES IN SCHEMA "some_schema" GRANT SELECT ON TABLES TO <username>;
Restrict access to columns (optional)
If you want to limit Fivetran’s access to the columns in your tables, grant the Fivetran user access to only certain columns. You must individually grant access for each column that you want to sync.
-
Revoke any previously granted permission to read all columns in the table.
REVOKE SELECT ON "some_schema"."some_table" FROM <username>;
-
Grant permission to the specific columns you want to sync (for example,
some_column
andother_column
).NOTE: If you chose XMIN as your incremental update mechanism, you must grant us access to the hidden system column
xmin
.GRANT SELECT (xmin, "some_column", "other_column") ON "some_schema"."some_table" TO <username>;
Once you restrict access to columns within a table, the Fivetran user will not have access to any new columns added to that table in the future. To grant access to new columns, you must rerun the command above.
Configure incremental update mechanismlink
Configure your chosen incremental update mechanism.
Logical replication with the pgoutput
plugin
To enable logical replication with the pgoutput
plugin, follow these steps:
-
Go to your PostgreSQL database.
-
Ensure that your server has ample free space for the logs. As soon as Fivetran processes a log, we delete it. However, we don’t delete logs if the sync is interrupted (for example, if we lose access to your database). In this case, logs may accumulate on your server and consume additional storage. The amount of additional disk space that these logs consume is proportional to the number of changes committed on the server. If we can’t resume a lost connection quickly enough and you need more disk space, you can drop the replication slot, which deletes its unconsumed logs.
-
Ensure that the
statement_timeout
setting on your server is either0
(the default value to disable the timeout) or greater than5 minute
. -
Set the
wal_level
parameter in your database configuration tological
. For a standard PostgreSQL database, do this by adding awal_level = logical
line to thepostgresql.conf1
file. Restart the server for this change to take effect. -
Ensure that your
max_replication_slots
value is equal to or higher than the number of PostgreSQL connectors that use WAL plus the number of other replication slots your database uses. -
Set the
wal_sender_timeout
parameter in your database configuration to0
to disable the timeout. -
Add a record to your
pg_hba.conf
file that allows your database to authenticate Fivetran’s connection to the WAL. -
Go to the
postgresql.conf
file and ensure that themax_wal_senders
parameter, which specifies the maximum number of concurrent connections to the WAL, is at least twice the total number of logical replication slots. For example, if your database uses 11 replication slots in total, then themax_wal_senders
value must be 22 or greater. -
Log in to a PostgreSQL console (such as a SQL workbench or psql) as a superuser.
-
Create a logical replication slot for the database you want to sync by running the following command. You must use the standard output plugin
pgoutput
.IMPORTANT: The replication slot name
fivetran_pgoutput_slot
quoted throughout this guide is used purely as an example. The actual replication slot name should be unique for every connector using the same PostgreSQL cluster. Replication slot names cannot start with a number.SELECT pg_create_logical_replication_slot('fivetran_pgoutput_slot', 'pgoutput');
-
Create a publication for your tables. If you want, you can create a publication for only certain tables so that you add or remove tables from the publication later on. Only changes from tables in the publication are replicated to Fivetran. Each database can have multiple distinct publications. You must have
CREATE
privileges or above to run this command.IMPORTANT: The publication name
fivetran_pub
quoted throughout this guide is used purely as an example. The actual publication name should be unique for every database and cannot start with a number.CREATE PUBLICATION fivetran_pub FOR TABLE table2, table4, table8;
To add or remove a table from a publication, run the following command. You must have ownership rights over the table(s).
ALTER PUBLICATION fivetran_pub ADD/DROP TABLE table_name;
Alternatively, you can create a publication for all of your tables. However, you cannot remove any table from this publication later on. You must have superuser privileges to run this command.
CREATE PUBLICATION fivetran_pub FOR ALL TABLES;
(Optional) You can choose which operations to include in the publication. For example, the following publication includes only
INSERT
andUPDATE
operations.CREATE PUBLICATION insert_only_pub FOR TABLE table1 WITH (publish = 'INSERT, UPDATE');
-
Verify that your chosen tables are in the publication.
SELECT * FROM pg_publication_tables.
-
Grant the Fivetran user permission to read the replication slot.
ALTER ROLE <username> WITH REPLICATION;
-
Log in as the Fivetran user.
-
Verify that the Fivetran user can read the replication slot by running the following command. Replace
fivetran_pgoutput_slot
with your replication slot name andfivetran_pub
with the publication name.SELECT count(*) FROM pg_logical_slot_peek_binary_changes('fivetran_pgoutput_slot', null, null, 'proto_version', '1', 'publication_names', 'fivetran_pub');
If the query succeeds, then permissions are sufficient.
IMPORTANT: You must periodically tune the
checkpoint_timeout
andmax_wal_size
parameters based on your PostgreSQL database operations. If you do not, you may experience replication failures. To learn how to tune, read this tuning checkpoints documentation.
Logical replication with the test_decoding
plugin
To enable logical replication with the test_decoding
plugin, follow these steps:
-
Go to your PostgreSQL database.
-
Ensure that your server has ample free space for the logs. As soon as Fivetran processes a log, we delete it. However, we don’t delete logs if the sync is interrupted (for example, if we lose access to your database). In this case, logs may accumulate on your server and consume additional storage. The amount of additional disk space that these logs consume is proportional to the number of changes committed on the server. If we can’t resume a lost connection quickly enough and you need more disk space, you can drop the replication slot, which deletes its unconsumed logs.
-
Ensure that the
statement_timeout
setting on your server is either0
(the default value to disable the timeout) or greater than5 minute
. -
Set the
wal_level
parameter in your database configuration tological
. For a standard PostgreSQL database, do this by adding awal_level = logical
line to thepostgresql.conf1
file. Restart the server for this change to take effect. -
Ensure that your
max_replication_slots
value is equal to or higher than the number of PostgreSQL connectors that use WAL plus the number of other replication slots your database uses. -
Set the
wal_sender_timeout
parameter in your database configuration to0
to disable the timeout. -
Add a record to your
pg_hba.conf
file that allows your database to authenticate Fivetran’s connection to the WAL. -
Go to the
postgresql.conf
file and ensure that themax_wal_senders
parameter, which specifies the maximum number of concurrent connections to the WAL, is at least twice the total number of logical replication slots. For example, if your database uses 11 replication slots in total, then themax_wal_senders
value must be 22 or greater. -
Log in to a PostgreSQL console (such as a SQL workbench or psql) as a superuser.
-
Create a logical replication slot for the database you want to sync by running the following command. You must use the output plugin
test_decoding
supplied in thepostgresql-contrib
subpackage.IMPORTANT: The replication slot name
fivetran_replication_slot
quoted throughout this guide is used purely as an example. The actual replication slot name should be unique for every connector using the same PostgreSQL cluster. Replication slot names cannot start with a number.SELECT pg_create_logical_replication_slot('fivetran_replication_slot', 'test_decoding');
-
Grant permission to the Fivetran user for reading the replication slot.
ALTER ROLE <username> WITH REPLICATION;
-
Log in as the Fivetran user.
-
Verify that the Fivetran user can read the replication slot by running the following command:
SELECT count(*) FROM pg_logical_slot_peek_changes('fivetran_replication_slot', null, null);
If the query succeeds, then permissions are sufficient.
IMPORTANT: You must periodically tune the
checkpoint_timeout
andmax_wal_size
parameters based on your PostgreSQL database operations. If you do not, you may experience replication failures. To learn how to tune, read this tuning checkpoints documentation.
XMIN
The XMIN method is based on the hidden xmin
system column that is present in all PostgreSQL tables. To enable XMIN, ensure that the statement_timeout
setting on your server is either 0
(the default value to disable the timeout) or greater than 5 minute
.
Fivetran Teleport Sync BETA
If you are trying to connect with a standby or read replica, run the following SQL commands:
CREATE AGGREGATE BIT_XOR(IN v bigint) (SFUNC = int8xor, STYPE = bigint);
If you are not connecting with a read replica, you do not need to to do any additional configuration. The aggregate that the Teleport mechanism will later use is automatically created for you.
Finish Fivetran configurationlink
-
In your connector setup form, enter a destination schema prefix. This prefix applies to each replicated schema and cannot be changed once your connector is created.
-
In the Host field, enter your database host’s IP (for example,
1.2.3.4
) or domain (for example,your.server.com
). -
Enter your database instance’s port number. The port number is usually
5432
. -
Enter the Fivetran-specific user that you created in Step 3.
-
Enter the password for the Fivetran-specific user that you created in Step 3.
-
Enter the name of your database (for example,
your_database
). -
Choose your connection method. If you selected Connect via an SSH tunnel, provide the following information:
- SSH hostname (do not use a load balancer’s IP address/hostname)
- SSH port
- SSH user
-
Choose your update method. If you selected Logical replication of the WAL using the test_decoding plugin, enter the name of your database’s replication slot. If you selected Logical replication of the WAL using the pgoutput plugin, enter both the name of your database’s replication slot and publication name accordingly.
-
Click Save & Test. Fivetran tests and validates our connection to your PostgreSQL database. Upon successful completion of the setup tests, you can sync your data using Fivetran.
Setup testslink
Fivetran performs the following tests to ensure that we can connect to your generic PostgreSQL database and that it is properly configured:
- The Connecting to SSH Tunnel Test validates the SSH tunnel details you provided in the setup form. It then checks that we can connect to your database using the SSH Tunnel. (We skip this test if you aren’t connecting using SSH.)
- The Connecting to Host Test validates the database credentials you provided in the setup form. The test verifies that the host is not private and then checks the connectivity to the host.
- The Validating Certificate Test generates a pop-up window where you must choose which certificate you want Fivetran to use. It then validates that certificate and checks that we can connect to your database using TLS. (We skip this test if you aren’t connecting directly.)
- The Connecting to Database Test checks that we can access your database.
- The Connecting to WAL Replication Slot Test confirms that the database associated with the replication slot matches the name you supplied in the setup form. It then verifies that the replication slot uses the
pgoutput
if you selected WAL with the pgoutput update method, ortest_decoding
plugin if you selected WAL with the test_decoding update method. Lastly, it makes sure that the Fivetran user has replication privileges. (We skip this test if you selected XMIN as your incremental update mechanism) - The Checking Configuration Values Test checks a set of WAL-configured values against the recommended settings and detects if they are below the recommended range. (We skip this test if you selected XMIN as your incremental update mechanism.)
- The Publication Test verifies that the supplied publication name exists in your database. (We skip this test if you selected XMIN or WAL with the test_decoding plugin as your incremental update mechanism.)
- The Validating Speed Setup test validates Fivetran can fetch data from your source database quickly enough. During this test, we measure our ability to download sample data from your source database to Fivetran, but we do not perform a full sync. We start a timer, then download the sample data in memory. We then calculate the connector speed based on how much data we downloaded and how long it took to download. The test shows a warning if the download speed is less than 5MB/sec.
NOTE: The tests may take a few minutes to finish running.
Related Contentlink
description Connector Overview
account_tree Schema Information
settings API Connector Configuration