getting this error in my pyhon code psycopg2.OperationalError: fe_sendauth: no password supplied

huangapple go评论99阅读模式
英文:

getting this error in my pyhon code psycopg2.OperationalError: fe_sendauth: no password supplied

问题

I try to run python app with docker-compose and the app using postgres as container, but when i run docker-compose up the Database run successfully but the app allways getting error "psycopg2.OperationalError: fe_sendauth: no password supplied".

The setting of the postgres using a init.sql file that i've been created and copy to the container in a directory that run this file automatically.

  1. import psycopg2
  2. import psycopg2.extras
  3. import os
  4. from dotenv import load_dotenv
  5. dotenv_path = os.path.join(os.path.dirname(__file__), '..', '.env')
  6. load_dotenv(dotenv_path)
  7. db_username = os.environ.get('POSTGRES_USER')
  8. db_password = os.environ.get('POSTGRES_PASSWORD')
  9. postgres_port = os.environ.get('POSTGRES_PORT')
  10. database_name = os.environ.get('POSTGRES_DB')
  11. # Connect to the PostgreSQL database
  12. conn = psycopg2.connect(
  13. host="db",
  14. port=postgres_port,
  15. database=database_name,
  16. user=db_username,
  17. password=db_password
  18. )
  19. # get the data from the DB in json format
  20. cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
  21. # select data from DB
  22. def get_data(query):
  23. try:
  24. cur.execute(query)
  25. data = cur.fetchall()
  26. return data
  27. except Exception as error:
  28. print(error)
  29. # update the DB
  30. def set_data(query):
  31. try:
  32. cur.execute(query)
  33. conn.commit()
  34. return "Database was updated"
  35. except Exception as error:
  36. print(error)
  1. version: "3.8"
  2. services:
  3. db:
  4. build: ./database/
  5. container_name: todo-db
  6. environment:
  7. POSTGRES_USER: ${POSTGRES_USER}
  8. POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
  9. POSTGRES_DB: ${POSTGRES_DB}
  10. restart: always
  11. ports:
  12. - 5432:5432
  13. volumes:
  14. - ./database/data:/var/lib/postgresql/data
  15. app :
  16. build: ./app/
  17. container_name: todo-app
  18. ports:
  19. - 8002:8000
  20. links:
  21. - db
  22. depends_on:
  23. - db
  24. pgadmin:
  25. container_name: pgadmin4_container
  26. image: dpage/pgadmin4
  27. restart: always
  28. environment:
  29. PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL}
  30. PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD}
  31. ports:
  32. - "5050:80"

Dockerfile of the python app:

  1. FROM python:3.9
  2. WORKDIR /code
  3. COPY ./requirements.txt /code/requirements.txt
  4. RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
  5. COPY . /code
  6. CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

Dockerfile of the postgres DB:

  1. FROM postgres:latest
  2. WORKDIR /app
  3. COPY init.sql /docker-entrypoint-initdb.d/init.sql
  4. EXPOSE 5432
  5. CMD [ "postgres" ]

init.sql file:

  1. CREATE USER IF NOT EXISTS todo_user WITH PASSWORD '${POSTGRES_PASSWORD}';
  2. CREATE DATABASE todo_db WITH OWNER todo_user;
  3. GRANT ALL PRIVILEGES ON DATABASE todo_db TO todo_user;
  4. \c todo_db todo_user;
  5. CREATE TABLE tasks(
  6. id SERIAL PRIMARY KEY,
  7. task VARCHAR NOT NULL,
  8. ststus VARCHAR NOT NULL
  9. );

.env file:

  1. POSTGRES_USER = todo_user
  2. POSTGRES_PASSWORD = Aa123456
  3. POSTGRES_DB = todo_db
  4. POSTGRES_PORT = 5432

I tried in the connection string in the app to set the default username and password of postgres database and it's working i successfully connect to database but when i use variable from .env file i'm getting the error.

In the postgres container i can see that the user and the DB created.

英文:

I try to run python app with docker-compose and the app using postgres as container, but when i run docker-compose up the Database run successfully but the app allways getting error "psycopg2.OperationalError: fe_sendauth: no password supplied".

The setting of the postgres using a init.sql file that i've been created and copy to the container in a directory that run this file automatically.

======================== connection to DB file ========================

  1. import psycopg2
  2. import psycopg2.extras
  3. import os
  4. from dotenv import load_dotenv
  5. dotenv_path = os.path.join(os.path.dirname(__file__), '..', '.env')
  6. load_dotenv(dotenv_path)
  7. db_username = os.environ.get('POSTGRES_USER')
  8. db_password = os.environ.get('POSTGRES_PASSWORD')
  9. postgres_port = os.environ.get('POSTGRES_PORT')
  10. database_name = os.environ.get('POSTGRES_DB')
  11. # postgres_host = os.environ.get('POSTGRES_HOST')
  12. print (database_name)
  13. # Connect to the PostgreSQL database
  14. conn = psycopg2.connect(
  15. host="db",
  16. port=postgres_port,
  17. database=database_name,
  18. user=db_username,
  19. password=db_password
  20. )
  21. # get the data from the DB in json format
  22. cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
  23. # select data from DB
  24. def get_data(query):
  25. try:
  26. # Run the query and fetch the results
  27. cur.execute(query)
  28. data = cur.fetchall()
  29. return data
  30. except Exception as error:
  31. print(error)
  32. # update the DB
  33. def set_data(query):
  34. try:
  35. # Run the query and fetch the results
  36. cur.execute(query)
  37. conn.commit()
  38. return "Database was updated"
  39. except Exception as error:
  40. print(error)

===================== docker-compose file =====================

  1. version: "3.8"
  2. services:
  3. db:
  4. build: ./database/
  5. container_name: todo-db
  6. environment:
  7. POSTGRES_USER: ${POSTGRES_USER}
  8. POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
  9. POSTGRES_DB: ${POSTGRES_DB}
  10. restart: always
  11. ports:
  12. - 5432:5432
  13. volumes:
  14. - ./database/data:/var/lib/postgresql/data
  15. # - ./database/init.sql:/docker-entrypoint-initdb.d/init.sql
  16. # env_file:
  17. # - ../TODO-App/.env
  18. app :
  19. build: ./app/
  20. container_name: todo-app
  21. ports:
  22. - 8002:8000
  23. links:
  24. - db
  25. depends_on:
  26. - db
  27. pgadmin:
  28. container_name: pgadmin4_container
  29. image: dpage/pgadmin4
  30. restart: always
  31. environment:
  32. PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL}
  33. PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD}
  34. ports:
  35. - "5050:80"

============================== Dockerfile of the python app ==============================

  1. FROM python:3.9
  2. #
  3. WORKDIR /code
  4. #
  5. COPY ./requirements.txt /code/requirements.txt
  6. #
  7. RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
  8. #
  9. COPY . /code
  10. #
  11. CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

=============================== Dockerfile of the postgres DB ===============================

  1. FROM postgres:latest
  2. WORKDIR /app
  3. COPY init.sql /docker-entrypoint-initdb.d/init.sql
  4. EXPOSE 5432
  5. CMD [ "postgres" ]

============== init.sql file ==============

  1. CREATE USER IF NOT EXISTS todo_user WITH PASSWORD '${POSTGRES_PASSWORD}';
  2. CREATE DATABASE todo_db WITH OWNER todo_user;
  3. GRANT ALL PRIVILEGES ON DATABASE todo_db TO todo_user;
  4. \c todo_db todo_user;
  5. CREATE TABLE tasks(
  6. id SERIAL PRIMARY KEY,
  7. task VARCHAR NOT NULL,
  8. ststus VARCHAR NOT NULL
  9. );

=========== .env file ===========

  1. POSTGRES_USER = todo_user
  2. POSTGRES_PASSWORD = Aa123456
  3. POSTGRES_DB = todo_db
  4. POSTGRES_PORT = 5432

I tried in the connection string in the app to set the default username and password of postgres database and it's working i successfully connect to database but when i use variable from .env file i'm getting the error.

In the postgres coantainer i can see that the user and the DB created.

答案1

得分: 0

看起来你只在"db"容器中使用环境变量,而没有在"app"容器中使用。

假设你的用于获取环境变量的Python代码有效,将envvars 添加到你的应用容器中:

  1. app:
  2. build: ./app/
  3. container_name: todo-app
  4. environment:
  5. POSTGRES_USER: ${POSTGRES_USER}
  6. POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
  7. POSTGRES_DB: ${POSTGRES_DB}
  8. ports:
  9. - 8002:8000
  10. links:
  11. - db
  12. depends_on:
  13. - db
英文:

Looks like you're only using environment variables for your "db" container, but not for the "app" container.

Provided that your python code for getting environment variables works, add envvars to your app container:

  1. app:
  2. build: ./app/
  3. container_name: todo-app
  4. environment:
  5. POSTGRES_USER: ${POSTGRES_USER}
  6. POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
  7. POSTGRES_DB: ${POSTGRES_DB}
  8. ports:
  9. - 8002:8000
  10. links:
  11. - db
  12. depends_on:
  13. - db

huangapple
  • 本文由 发表于 2023年3月31日 22:46:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定