Jump to content

[Solved] How to connect postgres from a flask app


Recommended Posts

Hi again,

I need help connecting my flask app to the postgres database on the server.

PS. I'm not trying to connect from outside although having remote access would be a bonus 


username: geojoe

database: geojoe_flask-app

database user: geojoe_admin

server: Tommy



Issue 2: how do I hide the flask.wsgi from urls

e.g instead of flask.geoffery.ml/flask.wsgi/login, i want it to be   flask.geoffery.ml/login


thanks in anticipation

Link to comment
Share on other sites

I don't know if anyone has ever done this here honestly. We only have a few people who use Python, even fewer of those use flask, and to my knowledge exactly zero use flask with postgres. Sadly you're unlikely to get help for this one, if anything you'll be told to use mysql instead.


Krydos can probably answer #2 for you (.htaccess file comes to mind, but I have no idea whether that would work with flask/python).


He can also enable postgres remote access for you. What IP would need access to it (or should we allow any)?

Link to comment
Share on other sites

Thanks for the reply.

Yes I would like access from any ip address.

The right way to connect to postgres through flask is as follows


In my case that would be




But it doesn't seem to work I think I may not have the right permissions to read from or write to the database, if it works I would be very grateful otherwise I might have to settle for the one from heroku

Link to comment
Share on other sites

I tested flask (python 3.7) with postgresql and it's working just fine for me. Here is some example code


import psycopg2

conn_string = "host=localhost port=5432 dbname=krydos_test user=krydos_user password=password123"
conn = psycopg2.connect(conn_string)
cur = conn.cursor()
cur.execute("select stuff from things where this = 'that'")
row = cur.fetchone()

database: geojoe_flask-app

database user: geojoe_admin

Neither your database, nor your user exist:

Link to comment
Share on other sites

Hi @RootAdmin I've done it 


I had sequence fields created and it turns out that postgres sequences needed special privileges to work, the problem arose when i try an insert operation; it gave the following error

Error in query: ERROR: permission denied for sequence users_user_id_seq

so i did a little research and was able to fix it


here's how i did it in case someone else has the same issue:


  • I opened phppgadmin
  • logged in
  • clicked on schema
  • selected my schema (i used the default public schema)
  • selected sequences 
  • clicked on privileges 
  • checked SELECT, UPDATE, DELETE for my user


and evoila it worked perfectly 


thanks your service - although free - is one of the best I've ever gotten 

Link to comment
Share on other sites

Please enable remote connection thanks

For which user? geojoe or geojoe_geoffery?


For the URLs If you do a link like this:

Then you end up at https://krydos.heliohost.org/flask/flask.wsgi/python/version/


But if you do the link like this:

Then you end up in the same place, but with a prettier url https://krydos.heliohost.org/flask/python/version/
Link to comment
Share on other sites

  • 3 weeks later...


I've solved the url problem and here's how

class ScriptNameStripper(Flask):
    def __call__(self, environ, start_response):
        environ['SCRIPT_NAME'] = ''
        return (super(ScriptNameStripper, self)
                .__call__(environ, start_response))
app = ScriptNameStripper(__name__)
  • Like 1
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Create New...