# Multi Region Postgres Allowing Writes on All Nodes

**URL:** <https://community.fly.io/t/multi-region-postgres-allowing-writes-on-all-nodes/4056>\
**Category:** Questions / Help\
**Created:** [February 12, 2022, 4:09pm UTC](https://community.fly.io/t/multi-region-postgres-allowing-writes-on-all-nodes/4056 "2022-02-12T16:09:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Piechowski](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/alex_piechowski/32/39888_2.png) [@Alex\_Piechowski](https://community.fly.io/u/Alex_Piechowski)\
**Post date:** [February 12, 2022, 4:09pm UTC](https://community.fly.io/t/multi-region-postgres-allowing-writes-on-all-nodes/4056/1 "2022-02-12T16:09:39Z")

</div>

When I connect in a region that should be read only, it still allows writing to the reader node.

`fly status`:

```auto
➜ lucky_jumpstart git:(hot-cuz-im-fly) ✗ fly status -a tmp-postgres
App
  Name = tmp-postgres
  Owner = personal
  Version = 2
  Status = running
  Hostname = tmp-postgres.fly.dev

Instances
ID PROCESS VERSION REGION DESIRED STATUS HEALTH CHECKS RESTARTS CREATED
ecd9c017 app 2 ord run running (replica) 3 total, 3 passing 0 1h13m ago
60357b81 app 2 scl run running (leader) 3 total, 3 passing 0 1h14m ago

```

Which denotes that it is a replica in the status.

When I combine the host of the DATABASE\_URL and FLY\_REGION, I get `ord.top2.nearest.of.tmp-postgres.internal`, which is allowing me to write `ord.tmp-postgres.internal` also allows me to write.

Shouldn’t the replica be throwing a “read only” error when I try to write to this node?

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [February 12, 2022, 4:12pm UTC](https://community.fly.io/t/multi-region-postgres-allowing-writes-on-all-nodes/4056/2 "2022-02-12T16:12:32Z")

</div>

Port 5432 on every nodes forwards you to the primary, writable postgres instance.

Port 5433 connects you directly to postgres replicas.

So the connection string you want for your primary region is:

```auto
top1.nearest.of.tmp-postgres.internal:5432

```

And in your replica regions:

```auto
top1.nearest.of.tmp-postgres.internal:5433

```

---

<div class="post-metadata">

**Author:** ![Alex\_Piechowski](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/alex_piechowski/32/39888_2.png) [@Alex\_Piechowski](https://community.fly.io/u/Alex_Piechowski)\
**Post date:** [February 12, 2022, 4:15pm UTC](https://community.fly.io/t/multi-region-postgres-allowing-writes-on-all-nodes/4056/3 "2022-02-12T16:15:44Z")

</div>

AHHH perfect, I glossed over that in the docs!

> [@kurt](#):
>
> ```auto
> top1.nearest.of.tmp-postgres.internal:5433
> 
> ```

Do I not need to region name itself? Example from the docs:

```auto
class Fly
  def self.database_url
    primary = ENV["PRIMARY_REGION"]
    current = ENV["FLY_REGION"]
    db_url = ENV["DATABASE_URL"]

    if primary.blank? || current.blank? || primary == current
      return db_url
    end

    u = URI.parse(db_url)
    u.hostname = "#{current}.#{u.hostname}"
    u.port = 5433

    return u.to_s
  end
end

```

---

<div class="post-metadata">

**Author:** ![kurt](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/kurt/32/43757_2.png) [@kurt](https://community.fly.io/u/kurt)\
**Post date:** [February 12, 2022, 4:16pm UTC](https://community.fly.io/t/multi-region-postgres-allowing-writes-on-all-nodes/4056/4 "2022-02-12T16:16:31Z")

</div>

I just fixed those docs. 😉

`top1.nearest.of.tmp-postgres.internal` is new since we first wrote those docs, so all you need to do now is change the port and you’re good.

---

<div class="post-metadata">

**Author:** ![Alex\_Piechowski](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/alex_piechowski/32/39888_2.png) [@Alex\_Piechowski](https://community.fly.io/u/Alex_Piechowski)\
**Post date:** [February 12, 2022, 4:20pm UTC](https://community.fly.io/t/multi-region-postgres-allowing-writes-on-all-nodes/4056/5 "2022-02-12T16:20:08Z")

</div>

> [@kurt](#):
>
> I just fixed those docs. 😉

~~Hmm, don’t see the fixups but that’s okay. Looking here: [https://fly.io/docs/getting-started/multi-region-databases/~~](https://fly.io/docs/getting-started/multi-region-databases/~~) must have just needed to deploy

> [@kurt](#):
>
> so all you need to do now is change the port and you’re good.

Same condition? when `ENV["PRIMARY_REGION"] != ENV["FLY_REGION"]`?

---

<div class="post-metadata">

**Author:** ![Alex\_Piechowski](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/alex_piechowski/32/39888_2.png) [@Alex\_Piechowski](https://community.fly.io/u/Alex_Piechowski)\
**Post date:** [February 12, 2022, 5:08pm UTC](https://community.fly.io/t/multi-region-postgres-allowing-writes-on-all-nodes/4056/6 "2022-02-12T17:08:19Z")

</div>

Still isn’t working, but I’ve found breadcrumbs:

`fly postgres attach` added `top2` instead of `top1`.

By switching this to `top1`, it now works as expected

Seems like a possible bug
