Is it possible to write a Pandas dataframe to PostgreSQL database using psycopg2?
Endgoal is to be able to write a Pandas dataframe to Amazon RDS PostgreSQL instance.
Establishing connection using python The connection class of the psycopg2 represents/handles an instance of a connection. You can create new connections using the connect() function. This accepts the basic connection parameters such as dbname, user, password, host, port and returns a connection object.
Instead of uploading your pandas DataFrames to your PostgreSQL database using the pandas. to_sql() function, you can write the data to a CSV file and COPY the file into PostgreSQL, which is considerably faster, as I'll demonstrate below.
If you use a SQLAlchemy engine around psycopg2, you could probably use df.to_sql
. See: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_sql.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With