What is the correct process to change the postgres leader region?

Hey there,

So it’s a bit of a process, but i’ll walk you through it.

Step 1: Adjust your apps primary region.

Pull down your fly.toml file if you haven’t already.

fly config save --app <app-name>

Modify the PRIMARY_REGION value inside of your fly.toml file

[env]
 PRIMARY_REGION = "lax"

Deploy your app.

WARNING: Your app will not accept writes until your issue the failover in Step 2. The HAProxy routes connections to the primary and leverages the PRIMARY REGION env var. This also requires an immediate deploy, which means it will deploy the new image to all members at the same time. This will result in a brief period of downtime. I would recommend testing this process on a staging environment if this app is critical.

# Run this inside the same directory as your fly.toml
fly deploy . --image flyio/postgres:<major-version> --strategy=immediate

If you don’t the image you’re running, you can view it by running:
fly image show

So for example, if you’re Tag indicates 14.2, the image reference should look like flyio/postgres:14.

Step 2: Orchestrating a failover

Verify your version:

fly image show

Registry   = registry-1.docker.io
Repository = flyio/postgres
Tag        = 14.2
Version    = v0.0.21
Digest     = sha256:4e4a7bfef439b5e02fa3803c4b8225b57c297fa114f995855d5d7807828d9008

If you’re running PG13/14 with Version v0.0.13+

fly ssh console --app <app-name>

pg-failover

If you’re running PG12 or an earlier Version:

fly ssh console --app <app-name>

bash
# Export Stolon specific env vars.
export $(cat /data/.env | xargs) 

# Identify the master keeper id
stolonctl status  

# Fail the master keeper to trigger the failover.
stolonctl failkeeper <master-keeper-id>

# Verify the state of the world.
stolonctl status  # Verify that the master has indeed changed.

Let me know if you have any questions on anything.

5 Likes