R
Railway2w ago
jins

Connect to public or private DB URL from app?

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
Solution:
pgbouncer should be connecting to your database via the private network and your app should be connecting to pgbouncer via the private network too
Jump to solution
9 Replies
Percy
Percy2w ago
Project ID: c0958e3e-4cd8-4f89-9376-7c4003820f57
jins
jins2w ago
c0958e3e-4cd8-4f89-9376-7c4003820f57
Solution
Brody
Brody2w 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
jins
jins2w ago
okay this is working:
DATABASE_URL=${{PgBouncer.DATABASE_PRIVATE_URL}}?pgbouncer=true
DATABASE_URL_DIRECT=${{myapp-db.DATABASE_URL}}
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`.
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`.
Brody
Brody2w ago
you do not want to connect to the public url as that would subject yourself to egress fees are you getting that error during build?
jins
jins2w 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}}
Brody
Brody2w ago
the private network is not available during build on the legacy builder, switch to the new builder in the service settings
jins
jins2w ago
amazing, that worked. Thanks so much!
Brody
Brody2w ago
no problem!