Solved - How to properly connect a Fly hosted .NET application to a Fly Postgres database

Hello,

I’m having problems connecting to my live database hosted on Fly.

Locally, I can connect to a database by first doing fly proxy, and then using the following connection string as required by Npgsql

"DefaultConnection": "Host=localhost;Database=db;Username=username;Password=pw"

But with the live API and live DB, I don’t think I can use localhost as a hostname, because they’re hosted on different containers so don’t share the same localhost right?

It’s here that I’m stuck. I have a fly secret that provides the connection string. When I change localhost to my database name, I get An error occurred using the connection to database 'postgres' on server ''.

I try to connect to it using pgAdmin without success. I think I must use a fly proxy to connect to it through localhost, but it seems odd to me that to do dev work I need to first run that command before my API can connect to the DB. Is that usual?

Sorry for the potentially noob question, I’ve only really used Azure DBs but I’m wanting to move everything over to Fly now as it’s so good!

Cheers

Solved it, with big thanks to How to connect .Net app to Postges @CunningLobster

Though I read this post first, I missed the key part of it, running fly attach.

So now as far as I understand it, for an API and DB hosted on fly, you use a connection string in the form of

Server={updatedHost};Port={pgPort};User Id={pgUser};Password={pgPass};Database={pgDb};

However locally, I’ll just run fly proxy and use a connection string in the form of

Host=localhost;Database=database;Username=username;Password=pw

I still don’t fully understand what’s going on but I’ll look into it some more.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.