This example spins up two pgEdge containers (postgres-n1, postgres-n2) and configures logical replication between them using Spock. A database called example_db is created on both nodes and changes replicate bi-directionally.
- Install Docker and Docker Compose on your host system.
- Ensure that port 6432 and 6433 are available on your host system.
- Ensure that the host system has internet access to pull container images.
The docker-compose.yaml file in this repository creates a Postgres database named example_db that is replicated between two pgEdge nodes. Before running this file, ensure that you have an installed and running copy of docker with Internet access.
Then, to deploy this example, use the command:
docker compose up -dYou can interact with the database on each node of your two-node cluster with psql. For convenience, open two terminal windows, and use the following commands to connect to each node. To open a psql session on the first node, run:
docker compose exec postgres-n1 psql -U admin example_dbLikewise, to open a psql session on the second node, run:
docker compose exec postgres-n2 psql -U admin example_dbTo demonstrate that the nodes are replicating, you can confirm that a row is replicated from a table on one node to the same table on the other node.
- Create a table on the first node:
docker compose exec postgres-n1 psql -U admin example_db -c "create table example (id int primary key, data text);"- Insert a row into our new table on the second node:
docker compose exec postgres-n2 psql -U admin example_db -c "insert into example (id, data) values (1, 'Hello, pgEdge!');"- See that the new row has replicated back to the first node:
docker compose exec postgres-n1 psql -U admin example_db -c "select * from example;"The Northwind sample dataset is a Postgres database dump that you can use to try replication with a more realistic database. To load the Northwind dataset into your pgEdge database, run:
curl https://downloads.pgedge.com/platform/examples/northwind/northwind.sql | docker compose exec -T postgres-n1 psql -U admin example_dbNow, try querying one of the new tables from the other node:
docker compose exec postgres-n2 psql -U admin example_db -c "select * from northwind.shippers"If you have psql, pgAdmin, or another Postgres client installed on your host machine, you can use these connection strings to connect to each node:
- First node: host=localhost port=6432 user=admin password=password dbname=example_db.
- Second node: host=localhost port=6433 user=admin password=password dbname=example_db.
For example, using psql:
psql 'host=localhost port=6432 user=admin password=password dbname=example_db'Properties specified in a service's environment define the deployment details. You can adjust these settings to customize the deployment.
environment:
PGEDGE_USER: pgedge
PGEDGE_PASSWORD: password
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
POSTGRES_DB: example_db
NODE_NAME: n1- POSTGRES_USER is the name of the database superuser; the default is admin.
- POSTGRES_PASSWORD is the password associated with the database superuser; the default is pgedge.
- POSTGRES_DB is the database name; the default is example_db.
- PGEDGE_USER is the name of the replication user; the default is pgedge.
- PGEDGE_PASSWORD is the password associated with the replication user; the default is password.
- NODE_NAME is the logical node name for the node; in our sample file, n1 and n2.
The ports section describes the ports in use by the node:
ports:
- target: 5432
published: 6432Our published ports are set to 6432 for postgres-n1 and 6433 for postgres-n2. Our .yaml file includes a clause that defines the creation of each node:
spock-node-n1:
content: |-
#!/usr/bin/env bash
set -Eeo pipefail
psql -v ON_ERROR_STOP=1 --username "pgedge" --dbname "example_db" \
-c "SELECT spock.node_create(node_name := 'n1', dsn := 'host=postgres-n1 port=5432 dbname=example_db user=pgedge password=password');"Note that this configuration only takes effect when the containers are first created. To recreate the database with a new configuration, stop the running example:
docker compose downAnd start it again:
docker compose up -d