0

I'm trying to connect to my docker postgresql image but SQLAlchemy in python is refusing the connection.

I've created a docker instance using the following command:

docker run  -d --name postgres-m2m -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=pass postgres

And I can successfully connect to my instance using Pycharm's Database right side panel. check the screen capture

So I'd tried to connect it with my python application.

First I did create the settings/config.py file :

class dbCredentials:
    DB_USER = 'postgres'
    DB_PASS  = 'pass'
    DB_NAME = 'postgres'
    DB_HOST = '0.0.0.0'
    DB_PORT = '5432'
    DB_SCHEMA = 'telecom'
    def __int__(self):
        pass

And then my model/base.py

from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from settings.config import dbCredentials

credentials = dbCredentials()

DB_USER = credentials.DB_USER
DB_PASS = credentials.DB_PASS 


DB_HOST = credentials.DB_HOST
DB_PORT = credentials.DB_PORT
DB_NAME = credentials.DB_NAME
DB_SCHEMA = credentials.DB_SCHEMA

engine = create_engine(f'postgresql://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}')
Session = sessionmaker(bind=engine)

Base = declarative_base(metadata=MetaData(schema=DB_SCHEMA))

After that I did create the table model/tables/status.py

from sqlalchemy import Column, SmallInteger, String
from src.models.base import Base

class Status(Base):
    __tablename__ = 'status'
    id = Column('id', SmallInteger, primary_key=True, autoincrement=True, nullable=False)
    STATUS = Column('STATUS', String, nullable=False)

    def __init__(self, STATUS):
        self.STATUS = STATUS

And finally my main.py file

from src.models.base import Base, Session, engine
from src.models.tables.status import Status
Base.metadata.create_all(engine, checkfirst=True)
session = Session()

At this point I'm getting the following error:

[...] conn = _connect(dsn, connection_factory=connection_factory, **kwasync) sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: role "postgres" does not exist (Background on this error at: http://sqlalche.me/e/e3q8)

Can anyone help me to solve this problem?

Thanks!

flaviodea6
  • 11
  • 3
  • Possible duplicate of [Connecting to Postgresql in a docker container from outside](https://stackoverflow.com/questions/37694987/connecting-to-postgresql-in-a-docker-container-from-outside) – J.J. Hakala Feb 26 '19 at 02:43
  • `psql` should be used first to test that connection to the database works. [tag:sqlalchemy] tag might be irrelevant here, and `0.0.0.0` is never approriate destination IP address when connecting a client program to some server instance. – J.J. Hakala Feb 26 '19 at 02:46

1 Answers1

0
  1. In the shared screenshot the database name is postgres but in the config.py file you have set it to stone. Is this intentional?
  2. You should not set the host as '0.0.0.0' as it listens on every available interface on your system. Try running the database on a specific interface like localhost or your <IP address> and update the config file accordingly.
Manish Dash
  • 2,004
  • 10
  • 20
  • Actually I took the screenshot before I made some changes on the code (I created another database with the pycharm database console). I'll update my question properly. Thank you for your help! I'll try to solve this by changing the IP addres! – flaviodea6 Feb 26 '19 at 10:15
  • The host solution worked for me! I'd found out that my port localhost:5432 was being used by my local postgres so I'd changed the docker to run in localhost:5433. using: docker run -d --name postgres-m2m -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=pass postgres and changing my config file – flaviodea6 Feb 27 '19 at 00:50