Connect postgres(psycopg2) with the main.py
See original GitHub issueI 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:
- Created 6 years ago
- Comments:9 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@anubhav3itb you shouldn’t connect to the Postgres DB with
localhostbut with the name of the Postgres container.So, the section:
Should be:
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