Can't connect to PostgreSQL database

I am building an app to visualize elections data. I have a database in postgres that works perfectly fine in local environment but when i deploy to streamlit community cloud it gives me this error: sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at “localhost” (, port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?

Could someone help me please? I already looked up in stackoverflow but nothing worked. Thank you!

From the error message, it appears you have your code pointing to some local on your computer (localhost). Once you deploy an app to Community Cloud, your app can’t see anything on your computer. You’ll need to have your database hosted somewhere that is internet accessible so your app can speak to your database through a (nonlocal) IP or web address.

If you need further assistance, it would help to see your code. (Make sure you don’t have any database secrets or credentials visible.)

That’s definitely the problem then. Thank you so much for your quick answer, I’m a newbie in terms of databases. In your experience, where would you recommend me to host the database if its not locally on PostgreSQL? It weights 8GB. Thank you in advance for your help, since more than 1 day I’ve been stuck, at least now I know why.

It depends on what your use case is. (Is this critical where you want a solid service level guarantee or are you looking for something as cheap as possible, ideally free, even if it might not be as reliable.)

I mostly work in Azure, so that would be my go-to. The big players as usual are AWS, Azure, and GCP, but I’d have to defer to others in the community where to go for the cheapest solutions…

great. Thank you a lot. I will deep dive into that options, the traffic of the site will only be high during election times here in Argentina, then it will be low so I want the cheapest solution. Thanks!!

1 Like

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