[파이썬] PostgreSQL 데이터 추가와 삽입

PostgreSQL is a powerful open-source relational database management system that provides a wide range of features for handling and manipulating data. In this blog post, we will explore how to add and insert data into a PostgreSQL database using Python.

Installing Required Packages

Before we proceed, make sure you have the psycopg2 package installed. You can install it using pip with the following command:

pip install psycopg2

Connecting to the PostgreSQL Database

To begin, we need to establish a connection to the PostgreSQL database using the psycopg2 package. Here’s an example of how to connect:

import psycopg2

# Establish a connection to the PostgreSQL database
conn = psycopg2.connect(
    database="your_database",
    user="your_username",
    password="your_password",
    host="your_host",
    port="your_port"
)

# Create a cursor object to execute SQL queries
cur = conn.cursor()

Make sure to replace "your_database", "your_username", "your_password", "your_host", and "your_port" with your actual database credentials.

Adding Data to an Existing Table

To add data to an existing table in PostgreSQL, we need to write an SQL INSERT statement and execute it using the cursor. Here’s an example:

# SQL statement for adding data to a table
sql_insert = """
    INSERT INTO your_table_name (column1, column2, column3)
    VALUES (%s, %s, %s)
"""

# Data to be added
data = ("Value 1", "Value 2", "Value 3")

# Execute the SQL statement with the data
cur.execute(sql_insert, data)

# Commit the transaction to save the changes
conn.commit()

Make sure to replace "your_table_name" with the actual name of the table you want to add data to.

Inserting Multiple Rows of Data

If you need to insert multiple rows of data into a table, you can use the executemany() method of the cursor. Here’s an example:

# SQL statement for adding multiple rows of data to a table
sql_insert_many = """
    INSERT INTO your_table_name (column1, column2, column3)
    VALUES (%s, %s, %s)
"""

# Data to be added
data = [
    ("Value 1", "Value 2", "Value 3"),
    ("Value 4", "Value 5", "Value 6"),
    ("Value 7", "Value 8", "Value 9")
]

# Execute the SQL statement with the data
cur.executemany(sql_insert_many, data)

# Commit the transaction to save the changes
conn.commit()

Closing the Connection

Once you are done adding data to the PostgreSQL database, it’s important to close the connection to free up resources. Here’s how you can do it:

# Close the cursor
cur.close()

# Close the connection
conn.close()

Conclusion

In this blog post, we learned how to connect to a PostgreSQL database using Python and add data to it. We explored adding data to an existing table as well as inserting multiple rows of data. Remember to always close the connection to the database once you are finished working with it.

Using Python to interact with PostgreSQL databases gives you the flexibility to automate data insertion tasks and make your application more powerful. Happy coding!