TCP/IP connection to Postgres Error

I have been having trouble connecting my Postgres database to my Streamlit app when using Streamlit sharing. I am able to deploy the app locally and connect to the database as expected, but when I deploy the app for sharing I keep getting the same error. I also followed the tutorial for connecting to PostgreSQL, creating the exact same database and .py file. This test app also runs locally as expected but gives the same error when trying to deploy for sharing. Here is the error:

I’m assuming this issue is being caused by my Postgres configuration, but have so far failed to determine what to change that allows the app to keep running locally and also be deployed for sharing. My PostgreSQL server’s connection properties match my app’s secrets:

Here is the test app I have been working with:

Same here! I am struggling with the exact same error. Tried switching my server to port 5433 as well-- no luck. Let me know if there is a workaround for connecting to a PostgreSQL DB in a deployed Streamlit app.

Hi @chrismsmallwood and @sage3141 -

Just to be clear, are you doing the setup of Postgres on Streamlit sharing? You won’t be able to connect to your local machine localhost, as Streaming sharing doesn’t have any way to know where ‘localhost’ is (locahost would refer to the Streamlit sharing container).


Yes, I tried to deploy via Streamlit Sharing. Ahhh I see so do I need to specify an IP instead of localhost or are you implying that it is not possible to connect to a server that is running locally?

Our infrastructure has no way of knowing what ‘localhost’ refers to, as we’re not connecting to your home computer in any way. ‘localhost’ in a networking sense refers to where a process is running, which in the case of Streamlit sharing is the Streamlit cloud infrastructure.

You can have a postgres instance running on Streamlit sharing if you set it up, or if you can make your local postgres instance available to the internet (not recommended), then you can specify a specific IP address to connect to.


Hi @randyzwitch,

Thank you for the explanation, that makes perfect sense why it is failing to connect to the database. What I am still unclear on is how to run a Postgres instance on Streamlit sharing rather than locally. Does this involve using a second container technology? Any additional resources or recommendations would be greatly appreciated!

Thanks for your help,

Depending on why you were using Postgres, you could try to create an instance on Streamlit sharing by using subprocess.Popen. The main downside to this is that there could be considerable computing resources needed for Postgres, which could potentially cause your container to continually crash.

But more importantly, if it DID work, you still have the issue that Streamlit sharing containers are 1) expected to be short-lived and 2) might be one of several replicas, depending on your app traffic. So if you are using a database for its data persistance capabilities, then any updates you save to the database are local to that specific container AND only as long as the container is alive.

So if you want to persist data for an indefinite amount of time, the Postgres instance should be running outside of the Streamlit sharing service. If you don’t need actual database capabilities, just saving the data, you could do something like writing to a Google Sheet which would be a free way to maintain your data.

Hope this helps,

1 Like