-1

Is there a way to connect/subscribe to Postgres logical replication/streaming replication using node or go? I know its a TCP/IP connection but not exactly where to start. I also know there is a package for this, was wondering for more of a vanilla/understanding solution.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Minh Nguyen
  • 140
  • 7

1 Answers1

2

I'm not certain what you want, but maybe you are looking for “logical decoding”.

If you want to directly speak the replication protocol with the server, you'll have to implement it in your code, but that information is pretty useless, as it only contains the physical changes to the data files.

If you want logical decoding, there is the test_decoding module provided by PostgreSQL, and here are some examples how it can be used.

Mind that test_decoding is for testing. For real-world use cases, you will want to use a logical decoding plugin that fits your needs, for example wal2json.

If that's what you want to consume, you'll have to look up the documentation for the logical decoding plugin you want to use to learn the format in which it provides the information.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • thanks Laurenz. I was looking for a way to connect to the stream of data using vanillajs or go. – Minh Nguyen Dec 04 '18 at 18:15
  • But unless you are using logical decoding, the stream will be of no value for you. It will contain information of the form *In file 12345, block 27, change bytes 42 to 99 to the following bytes: ...*. – Laurenz Albe Dec 04 '18 at 18:18
  • Right because thats what the WAL log is. Once set up, is there a way to connect this to a nodejs stream? I have set up so that there are logical decoding. – Minh Nguyen Dec 04 '18 at 19:18
  • The first two sentences of your last comment contradict the third one. Logical or physical? Anyway, I have extended the answer in the hope that that clarifies something. – Laurenz Albe Dec 04 '18 at 19:46
  • Sorry Im not getting the point across. I have been able to set up logical decoding of the WAL and been able to get data from it but only through postgres servers themselves. I was interested in the code for javascript/go to hook onto those streaming changes – Minh Nguyen Dec 05 '18 at 07:38
  • I understand now. Did you look at the documentation of the plugin you are using? Is my answer useful at all or should I delete it? – Laurenz Albe Dec 05 '18 at 09:02