Unable to connect flask app deployed on render to my postgreSQL database with localhost

I am trying to connect my Flask application, deployed on render.com, to a PostgreSQL database. The database is already working with my development app, but I am encountering an error when trying to connect from the deployed app.

I am using the normal format for DATABASE_URL=‘postgres:{username}@{host}:{port}/{database_name}’

I have made the following changes to the pg_hba.conf and postgresql.conf files, and have restarted with postgresql with brew:

pg_hba.conf:

local   all             all                                     md5
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
host    all             all             0.0.0.0/0               trust

postgresql.conf:

listen_addresses = '*'  # what IP address(es) to listen on;
                        # comma-separated list of addresses;
                        # defaults to 'localhost'; use '*' for all
                        # (change requires restart)

Despite these changes, I am still getting the following error when trying to connect:

Error connecting to database: (psycopg2.OperationalError) connection to server at "localhost" (::1), port 5432 failed: 
Connection refused Is the server running on that host and accepting TCP/IP connections? 
connection to server at "localhost" (127.0.0.1), port 5432 failed: 
Connection refused Is the server running on that host and accepting TCP/IP connections? 
(Background on this error at: https://sqlalche.me/e/20/e3q8)

Is there something else I need to configure or modify to successfully connect my Flask app to the PostgreSQL database on render.com?

Hi,

If you’re running your app on a Render service, referencing localhost would point to the Render instance.

I wouldn’t recommend trying to expose your local Postgres instance to the public internet.

Maybe create a Postgres instance on Render and point your app at it with the Internal Database URL. More in the docs: https://docs.render.com/databases#connecting-with-the-internal-url

Alan

1 Like

Thanks Alan! I did use a render database with the internal url and it worked, but I noticed it was lagging severely on the free tier compared to my development app + local instance of PostgreSQL, basically unusable, which is why I tried switching back.

But I see what you mean about exposure. any other ways I can address the lag I’m noticing with the render database?

Is it possible the “lag” you’re describing is a free instance type on a Web Service?

Free Web Service instance types will spin down after 15 minutes of inactivity and spin back up with a subsequent request - which can take a little time. This is noted in the docs: https://docs.render.com/free#spinning-down-on-idle.

And on the service dashboard:

Your free instance will spin down with inactivity, which can delay requests by 50 seconds or more. Upgrade now.

Any paid Web Service instance type will not have the spin-down behavior.

Alan

It seems even when spun up (a few minutes performing various actions on the app) clicking through from page to page, and sending messages using websockets is unusable with the lag. But I will try a paid tier and see if it resolves itself.

Thanks again Alan

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