Self-hosted Anvil - PostgreSQL listening port

After switching to self hosted Anvil, I noticed that embedded Postgres listening port is changing every time I start the anvil-app-server. Is it about Anvil or Postgres, and can I fix it?
Thanks in advance.

I’m not aware of any way to change that, but you can always use a separate database instead.

1 Like
import psycopg2
import anvil.tables

conn = psycopg2.connect(anvil.tables.get_connection_string())
with conn.cursor() as cur:
    ...

Are you trying to connect to it like this?
from here:

In the docs it says everything is temporary? Maybe that’s why the port changes?

The docs also talk about remote connections, like using an SSH tunnel, and how to specify your tunnel port passed into anvil.tables.get_connection_string()

1 Like

I think I will proceed with a “separate” db server rather than the embedded one -for other reasons too.
I guess you mean the “database” option (jdbc connection) for Anvil Runtime shown below, right?

I was planning to connect with pgAdmin desktop app remotely.
I can do it but it is annoying to change the port all the time (after every restart).

Yep. That’s exactly what I meant. I often run the app server in a docker container with a separate container for the db so I can access it independently.

2 Likes