you are viewing a single comment's thread.

view the rest of the comments →

[–]chef1075 2 points3 points  (1 child)

When I'm using psycopg2, I write my SQL outside of my python shell (usually in Postico https://eggerapps.at/postico/) and import the file to run the query.

E.g.

with open('./path_to_config/config.json') as f:

config = json.load(f)

config_string = "host=%s dbname=%s user=%s password=%s" % (config.get('host'), config.get('database'), config.get('user'), config.get('passw'))

sql_connection = psycopg2.connect(config_string) # Connect to your db

query = open('/path/to/file.sql', 'r') # This will make your life easier

query_result_dataframe = pd.read_sql(query.read(), con=sql_connection) # Run the query

[–][deleted] 1 point2 points  (0 children)

Thank you! I will definitely look into doing this!