Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RDS Connection to Python Using SQLalchemy

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!

like image 514
Alex Avatar asked Nov 14 '25 15:11

Alex


1 Answers

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 ,...)

like image 84
Ashkan Goleh Pour Avatar answered Nov 17 '25 06:11

Ashkan Goleh Pour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!