i've been trying to use my local postgresql database but for some reason, everytime that i try to get some data with the flask app in a docker container, i get an error connection .
docker-compose.yaml:
version: '3.3'
services:
web:
build: .
ports:
- 5000:5000
Dockerfile:
FROM python:3.8-alpine
COPY . /app
WORKDIR /app
RUN \
apk add postgresql-libs && \
apk add --virtual .build-deps gcc musl-dev postgresql-dev
RUN apk add build-base
ENV VIRTUAL_ENV=/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN pip install -r requirements.txt
ENV FLASK_APP=project/app.py
EXPOSE 5000
CMD ["flask", "run", "--host", "0.0.0.0"]
On my flask app configuration, i defined SQLALCHEMY_DATABASE_URI as
pg_user = "postgres"
pg_pwd = "1404"
pg_port = "5433"
SQLALCHEMY_DATABASE_URI = f"postgresql://{pg_user}:{pg_pwd}@localhost:{pg_port}/flaskproject"
And i started the postgresql service:
sudo service postgresql start
However, when i try to get to any endpoint that to access the database, i get an error:
flask-project-web-1 | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "localhost" (127.0.0.1), port 5433 failed: Connection refused
flask-project-web-1 | Is the server running on that host and accepting TCP/IP connections?
flask-project-web-1 | connection to server at "localhost" (::1), port 5433 failed: Address not available
flask-project-web-1 | Is the server running on that host and accepting TCP/IP connections?
The strange behaviour is that, if i try to run my server without docker, it works. I also tried connecting with postgree with ubuntu cmd and it worked:
psql -h localhost -p 5433 -d flaskproject -U postgres
I have changed the posgresql.conf file in the following way:
listen_addresses = '*'
port = 5433
I also have changed the pg_gba.conf file in the following way:
local all postgres trust
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
Despite all of that, it's still not working.