create-a-publication-other.html 2.7 KB

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