Connect to public or private DB URL from app?

AnonymousTRIAL

a year ago

I'm just coming back around to updating an old project sicne the DB migrations a few months back.

I have PgBouncer connected to my Postgres DB.

  • From my app I am connecting to the Public DB URL for PgBouncer

  • then PgBouncer connects to the Postgres DB via private internal connection (whatever was setup during that deployment).

I just need to confirm that the app connecting to the public DB url is correct.

Thanks

0 Replies

AnonymousTRIAL

a year ago

c0958e3e-4cd8-4f89-9376-7c4003820f57


a year ago

pgbouncer should be connecting to your database via the private network and your app should be connecting to pgbouncer via the private network too


AnonymousTRIAL

a year ago

okay this is working:

DATABASE_URL=${{PgBouncer.DATABASE_PRIVATE_URL}}?pgbouncer=true
DATABASE_URL_DIRECT=${{myapp-db.DATABASE_URL}}

I'm using prisma and their pgbouncer docs say to set the pgBouncer URL then connect direct to postgres as directUrl for Prisma Migrate

https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections/pgbouncer#prisma-migrate-and-pgbouncer-workaround

If I try to use the private Url for postgres for Direct I get an error but I think it should be fine connecting to public?

Datasource "db": PostgreSQL database "railway", schema "public" at "dbname.railway.internal:5432"

Error: P1001: Can't reach database server at `dbname.railway.internal:5432`

Please make sure your database server is running at `dbname.railway.internal:5432`.

a year ago

you do not want to connect to the public url as that would subject yourself to egress fees


a year ago

are you getting that error during build?


AnonymousTRIAL

a year ago

If I change this to DATABASE_URL_DIRECT=${{myapp-db.DATABASE_PRIVATE_URL}} in the variables and deploy I get that error in the build log and it fails

But it works with DATABASE_URL_DIRECT=${{myapp-db.DATABASE_URL}}


a year ago

the private network is not available during build on the legacy builder, switch to the new builder in the service settings


AnonymousTRIAL

a year ago

amazing, that worked. Thanks so much!


a year ago

no problem!


Connect to public or private DB URL from app? - Railway Help Station