question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connect postgres(psycopg2) with the main.py

See original GitHub issue

I tried to connect postgres db from main.py, but its giving OperationalError: received invalid response to SSL negotiation: H. This issue is because the port 5432 is not properly exposed.

DOCKERFILE

FROM tiangolo/uwsgi-nginx-flask:python2.7
ENV LISTEN_PORT 5432
EXPOSE 5432
COPY tmp.conf /etc/nginx/conf.d/tmp.conf
COPY ./app /app
RUN pip install -r /app/requirements.txt

main.py

from flask import Flask
import psycopg2
app = Flask(__name__)

@app.route("/")
def hello():
    db = psycopg2.connect(host='127.0.0.1', port=5432, user='postgres',
                          password='123', dbname='chatbotdev')
    return "Hello World from Flask"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

tmp.conf

server {
    listen 8080 default_server;
    listen [::]:8080 default_server;

    location / {
      try_files $uri @app;
    }

    location @app {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi.sock;
    }
}

requirements.txt psycopg2==2.7.3.1

Can someone please explain, how can I use postgres within this docker container, because I’m able to do this in other linux docker containers.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
tiangolocommented, Apr 5, 2018

@anubhav3itb you shouldn’t connect to the Postgres DB with localhost but with the name of the Postgres container.

So, the section:

    db = psycopg2.connect(host='localhost', port=5432, user='postgres',
                              password='123', dbname='chatbotdev')

Should be:

    db = psycopg2.connect(host='postgres', port=5432, user='postgres',
                              password='123', dbname='chatbotdev')

You can generate an example project that uses this image and PostgresSQL and has everything already configured with: https://github.com/tiangolo/full-stack

Or you can use it to check and compare the differences.

1reaction
JORDYSCHEMPARATHYcommented, Sep 15, 2020

@anubhav3itb you shouldn’t connect to the Postgres DB with localhost but with the name of the Postgres container.

So, the section:

    db = psycopg2.connect(host='localhost', port=5432, user='postgres',
                              password='123', dbname='chatbotdev')

Should be:

    db = psycopg2.connect(host='postgres', port=5432, user='postgres',
                              password='123', dbname='chatbotdev')

You can generate an example project that uses this image and PostgresSQL and has everything already configured with: https://github.com/tiangolo/full-stack

Or you can use it to check and compare the differences.

Saved my day. Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using psycopg2 with PostgreSQL
Psycopg2 is a mature driver for interacting with PostgreSQL from the Python scripting language. It is written in C and provides a means...
Read more >
Python PostgreSQL Tutorial Using Psycopg2 - PYnative
1. Install and import psycopg2 module. Import using a import psycopg2 statement so you can use this module's methods to communicate with the...
Read more >
Connect to PostgreSQL Database Server Using Python ...
The psycopg2 is the PostgreSQL connector commonly used by Python developers to connect to Python. It's the core module for this tutorial, so ......
Read more >
PostgreSQL Python: Connect To PostgreSQL Database Server
In this tutorial, you will learn how to connect to the PostgreSQL database server in Python using the psycopg 2 database adapter.
Read more >
Basic module usage — Psycopg 2.9.5 documentation
Psycopg converts Python variables to SQL values using their types: the Python type determines the function used to convert the object into a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found