Reputation: 383
I need to setup/manage a replication (PostgreSQL built-in replication or by means of third-party solutions like Slony) between two PostgreSQL instances programmatically with Java. Is there any Java API for this? Thanks in advance.
Upvotes: 0
Views: 1026
Reputation: 325141
Unless you're going to use a managed service like AWS RDS or Heroku Postgres, the only Java APIs for this are those that concern file access and process management. If you're using AWS RDS you can use the AWS Java SDK; for Heroku there may be something similar.
If you're working with stock directly-managed PostgreSQL instances you'll need to open/parse/modify/write any relevant config files, or use external tools to do so. For starting/stopping DBs you'll need to use the regular utilities like pg_ctl
via process invocation. If you need to work remotely on machines you'll probably want to use one of the Java SSH libraries.
So you'll be using things like ProcessBuilder
, java.io.InputStreamReader
, etc, possibly alongside remote shell tools like JSCH.
If you want to get fancy, you can use SSH as the transport between your master JVM and a JVM on each node, which is responsible for running processes and modifying files, and you can exchange serialized messages over the SSH channel for communication. While it's a bit more code to get it set up, this works extremely well and lets you "think in Java" to a greater degree than most other approaches.
Personally - I strongly suggest using Puppet, Chef, Ansible, etc to automate as much as possible, then just have your Java code invoke the automation tools. Also look into repmgr.
Upvotes: 3