create-a-publication-aws.html 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. Once logical replication is enabled, create a publication with the tables that
  2. you want to replicate to Materialize. You'll also need a user for Materialize
  3. with sufficient privileges to manage replication.
  4. 1. As a _superuser_, use `psql` (or your preferred SQL client) to connect to
  5. your database.
  6. 1. For each table that you want to replicate to Materialize, set the
  7. [replica identity](https://www.postgresql.org/docs/current/sql-altertable.html#SQL-ALTERTABLE-REPLICA-IDENTITY)
  8. to `FULL`:
  9. ```postgres
  10. ALTER TABLE <table1> REPLICA IDENTITY FULL;
  11. ```
  12. ```postgres
  13. ALTER TABLE <table2> REPLICA IDENTITY FULL;
  14. ```
  15. `REPLICA IDENTITY FULL` ensures that the replication stream includes the
  16. previous data of changed rows, in the case of `UPDATE` and `DELETE`
  17. operations. This setting enables Materialize to ingest PostgreSQL data with
  18. minimal in-memory state. However, you should expect increased disk usage in
  19. your PostgreSQL database.
  20. 1. Create a [publication](https://www.postgresql.org/docs/current/logical-replication-publication.html)
  21. with the tables you want to replicate:
  22. _For specific tables:_
  23. ```postgres
  24. CREATE PUBLICATION mz_source FOR TABLE <table1>, <table2>;
  25. ```
  26. _For all tables in the database:_
  27. ```postgres
  28. CREATE PUBLICATION mz_source FOR ALL TABLES;
  29. ```
  30. The `mz_source` publication will contain the set of change events generated
  31. from the specified tables, and will later be used to ingest the replication
  32. stream.
  33. Be sure to include only the tables you need. If the publication includes
  34. additional tables, Materialize will waste resources on ingesting and then
  35. immediately discarding the data.
  36. 1. Create a user for Materialize, if you don't already have one:
  37. ```postgres
  38. CREATE USER materialize PASSWORD '<password>';
  39. ```
  40. 1. Grant the user permission to manage replication:
  41. ```postgres
  42. GRANT rds_replication TO materialize;
  43. ```
  44. 1. Grant the user the required permissions on the tables you want to replicate:
  45. ```postgres
  46. GRANT CONNECT ON DATABASE <dbname> TO materialize;
  47. ```
  48. ```postgres
  49. GRANT USAGE ON SCHEMA <schema> TO materialize;
  50. ```
  51. ```postgres
  52. GRANT SELECT ON <table1> TO materialize;
  53. ```
  54. ```postgres
  55. GRANT SELECT ON <table2> TO materialize;
  56. ```
  57. Once connected to your database, Materialize will take an initial snapshot
  58. of the tables in your publication. `SELECT` privileges are required for
  59. this initial snapshot.
  60. If you expect to add tables to your publication, you can grant `SELECT` on
  61. all tables in the schema instead of naming the specific tables:
  62. ```postgres
  63. GRANT SELECT ON ALL TABLES IN SCHEMA <schema> TO materialize;
  64. ```