Home > Software design >  RDS Connection to Python Using SQLalchemy
RDS Connection to Python Using SQLalchemy

Time:05-24

I'd like to start out by saying I am very much a student and a noob. I've been struggling to create a connection from our AWS RDS Database. It is the host for our ML model in Jupyter Notebook. Attempting to use SQLalchemy as the connector.

Trying to utilize variables if possible to create the connection, and use getpass for security.

I've imported the following dependencies for the project:

import pandas as pd
import sqlalchemy
from sqlalchemy.ext.automap import automap_base`
from sqlalchemy.orm import Session`
from sqlalchemy import create_engine`
from sqlalchemy.exc import SQLAlchemyError`
from getpass import getpass`
from sklearn.preprocessing import OneHotEncoder, LabelEncoder

This is my code to connect:

secret = getpass('Enter the secret value: ')

engine = create_engine(
    host="anonymousendpoint.amazonaws.com",
    port='5432',
    database="DBname",
    user="Project",
    password="secret"
)

engine.connect()

Have tried including url='postgres' in the args above, and a few other variations. I get the following error the most: TypeError: create_engine() missing 1 required positional argument: 'url'

Sometimes this error: ArgumentError: Could not parse rfc1738 URL from string 'postgres'

It is prompting for the password, so I know it is at least running through the code. Thanks in advance for your help!

CodePudding user response:

try it , i hope helpfully

conf ={
    'host':"anonymousendpoint.amazonaws.com",
    'port':'5432',
    'database':"DBname",
    'user':"Project",
    'password':"secret"
}
engine = create_engine("postgresql://{user}:{password}@{host}:{port}/{database}".format(**conf))

postgresql DBApi(asyncpg,psycopg,pg800 ,...)

  • Related