geojoe Posted January 26, 2020 Posted January 26, 2020 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: geojoedatabase: geojoe_flask-appdatabase user: geojoe_adminserver: Tommy Issue 2: how do I hide the flask.wsgi from urlse.g instead of flask.geoffery.ml/flask.wsgi/login, i want it to be flask.geoffery.ml/login thanks in anticipation
wolstech Posted January 28, 2020 Posted January 28, 2020 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)?
geojoe Posted January 28, 2020 Author Posted January 28, 2020 Thanks for the reply.Yes I would like access from any ip address.The right way to connect to postgres through flask is as followsPostgres+psycopg2://db_username:db_password@db_host:5432/db_nameIn 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
Krydos Posted January 28, 2020 Posted January 28, 2020 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-appdatabase user: geojoe_adminNeither your database, nor your user exist: Databases: geojoe_flask Users: geojoe geojoe_geoffery
geojoe Posted January 28, 2020 Author Posted January 28, 2020 I changed the databases and username while I was trying to diagnose the problem Thank for the help. Please enable remote connection thanks
geojoe Posted January 28, 2020 Author Posted January 28, 2020 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 phppgadminlogged inclicked on schemaselected 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
Krydos Posted January 29, 2020 Posted January 29, 2020 Please enable remote connection thanksFor 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/
geojoe Posted January 29, 2020 Author Posted January 29, 2020 The remote connection isn't needed anymore And the urls start without flask.wsgi but when you return to the same page again it's thereBut it's cool
geojoe Posted February 15, 2020 Author Posted February 15, 2020 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__) 1
Recommended Posts