在这里拼命寻求帮助,一直在应用各种博客中建议的修复程序,但没有帮助。
要求。我需要连接到两个不同的postgres数据库。一个是主数据库,另一个是从辅助数据库仅加载5个模型(数据库视图)。
我们正在使用IAMprofiles连接到数据库。这导致每15分钟PAM身份验证失败,为了解决此问题,我们使用创建者(lambda)来管理连接。
当前的问题是主连接工作正常,但是辅助连接无法被识别,并且仍在主数据库而不是辅助数据库中查找架构。
尝试以下:
使用SQLALCHEMY_DATABASE_URI的主要连接 辅助使用绑定。 使用模型上的绑定来识别要与“ autoload_with”选项一起使用的数据库
由于某种原因,该异常表示传递的引擎没有名为“ drivername”的属性
任何帮助将不胜感激。
Config file:
class IAMProfileAPI:
db_type = 'rds'
cert_path = 'certificate'
aws_region = 'region'
rds_host = 'host'
rds_port = 5432
rds_db_name = 'database'
rds_user = 'database_user'
rds_ssl_mode = 'verify-ca'
rds_schema_name = 'target_dbo'
rds_pwd = ''
def get_IAM_token_api():
aws_client = boto3.client(IAMProfileAPI.db_type, region_name=IAMProfileAPI.aws_region)
token = aws_client.generate_db_auth_token(IAMProfileAPI.rds_host, IAMProfileAPI.rds_port, IAMProfileAPI.rds_user)
return token
def create_sql_engine_api():
IAMProfileAPI.rds_pwd = get_IAM_token_api()
conn_strold = f"postgresql+psycopg2://{IAMProfileAPI.rds_host}:{IAMProfileAPI.rds_port}/{IAMProfileAPI.rds_db_name}" \
f"?sslmode={IAMProfileAPI.rds_ssl_mode}&sslrootcert={IAMProfileAPI.cert_path}"
conn_str = 'postgresql+psycopg2://{user}:{pw}@{url}/{db}?sslmode={sslmode}&sslrootcert={sslcert}'.\
format(user=IAMProfileAPI.rds_user, pw=IAMProfileAPI.rds_pwd, url=IAMProfileAPI.rds_host,
db=IAMProfileAPI.rds_db_name,sslmode=IAMProfileAPI.rds_ssl_mode, sslcert=IAMProfileAPI.cert_path)
args = dict()
rds_credentials = {'connect_args': {'user': IAMProfileAPI.rds_user, 'password': get_IAM_token_api()}}
args.update(rds_credentials)
return conn_str
class IAMProfile:
db_type = 'rds'
cert_path = 'certificate'
aws_region = 'region'
rds_host = 'host'
rds_port = 5432
rds_db_name = 'database'
rds_user = 'database_user'
rds_ssl_mode = 'verify-ca'
rds_schema_name = 'target_dbo'
rds_pwd = ''
def create_sql_engine():
connection_string = f"postgresql+psycopg2://{IAMProfile.rds_host}:{IAMProfile.rds_port}/{IAMProfile.rds_db_name}" \
f"?sslmode={IAMProfile.rds_ssl_mode}&sslrootcert={IAMProfile.cert_path}"
return connection_string
def get_IAM_token():
aws_client = boto3.client(IAMProfile.db_type, region_name=IAMProfile.aws_region)
token = aws_client.generate_db_auth_token(IAMProfile.rds_host, IAMProfile.rds_port, IAMProfile.rds_user)
return token
connection_string = create_sql_engine()
connection_string_api = create_sql_engine_api()
db_schema = 'primary_dbo'
dbo_schema = 'secondary_dbo'
SQLALCHEMY_DATABASE_URI = connection_string
SQLALCHEMY_ENGINE_OPTIONS = {'creator': lambda: psycopg2.connect(database=IAMProfile.rds_db_name,
user=IAMProfile.rds_user,
host=IAMProfile.rds_host,
port=IAMProfile.rds_port,
password=get_IAM_token(),
sslmode=IAMProfile.rds_ssl_mode,
sslrootcert=IAMProfile.cert_path)}
backend_engine = create_engine(connection_string_api)
meta = MetaData(bind=backend_engine)
SQLALCHEMY_BINDS = {
#db_schema: connection_string
dbo_schema: connection_string_api
}
SQLALCHEMY_TRACK_MODIFICATIONS = False
Models.py
class LoadTheFields(Model):
___bind_key__ = 'dbo_schema'
__tablename__ = 'loadmainfields'
table_args__ = {'schema': config.dbo_schema , 'autoload': True, 'autoload_with': backendengine(dbo_schema)}
id = Column(Integer, primary_key=True)`dbo_schema`