Jump to content

[Solved] How to connect postgres from a flask app


geojoe

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

Postgres+psycopg2://db_username:db_password@db_host:5432/db_name

In my case that would be

 

Postgres+psycopg2://geojoe_geoffery:donsniper123@localhost:5432/geojoe_flask

 

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

#!/usr/bin/python3.7

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()
print(row)

database: geojoe_flask-app

database user: geojoe_admin

Neither your database, nor your user exist:

Databases:
 geojoe_flask
Users:
 geojoe
 geojoe_geoffery
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:

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

 

But if you do the link like this:

href="/flask/python/version/"
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...

UPDATE: 

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

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