import pandas as pd
import psycopg2
from sqlalchemy import create_engine
df = pd.read_csv('your_file.csv')
conn = psycopg2.connect(
database="your_database_name",
user="your_username",
password="your_password",
host="your_host",
port="your_port"
)
engine = create_engine('postgresql+psycopg2://{0}:{1}@{2}/{3}'.format(
"your_username",
"your_password",
"your_host",
"your_database_name")
)
to_sql
method and specifying the engine, the table name, and the if_exists
parameter set to append or replace, depending on your needs:df.to_sql('your_table_name', engine, if_exists='append', index=False)
Note that you may also want to specify the data types for each column in the to_sql
call, using the dtype
parameter. This is particularly useful when dealing with timestamp data, in order to maintain the correct timezone information in the database.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-13 04:01:52 +0000
Seen: 15 times
Last updated: Jul 13 '23
How can the SQL debug mode be activated in PostgreSQL version 15.2?
What is the procedure for using pg_restore on Windows with Docker?
What is the equivalent of SQL Server NVARCHAR in PostgreSQL?
What is the method to retrieve the JSON data from a column in SQL?
How to solve the issue of being unable to establish a connection with libpq on Mac M1?
Is it possible to launch multiple instances of PostgreSQL using Spring Boot Testcontainers?
Due to SyntaxError, why am I unable to create a TIMESTAMP WITH TIMEZONE column in postgres?