[파이썬] Psycopg2에서 Batch operations 및 executemany()

Psycopg2 is a popular PostgreSQL adapter for Python that enables developers to interact with PostgreSQL databases. In this blog post, we will explore how to use batch operations and the executemany() method in Psycopg2.

Batch Operations

Batch operations allow you to execute multiple SQL statements as a single unit, minimizing the overhead of communication between your Python application and the PostgreSQL database. This is particularly useful when you need to perform repetitive tasks or insert multiple rows into a table.

To perform batch operations in Psycopg2, you need to create a list of tuples, where each tuple represents a set of values to be inserted or manipulated. Let’s look at an example:

import psycopg2

# Connect to the PostgreSQL database
conn = psycopg2.connect("dbname=mydb user=myuser password=mypassword")
cur = conn.cursor()

# Create a list of tuples with data to be inserted
data = [
    ('John', 'Doe'),
    ('Jane', 'Smith'),
    ('Michael', 'Johnson')
]

# Execute the batch operation using executemany()
cur.executemany("INSERT INTO users (first_name, last_name) VALUES (%s, %s)", data)

# Commit the changes
conn.commit()

# Close the cursor and the connection
cur.close()
conn.close()

In the code above, we first establish a connection to the PostgreSQL database using psycopg2.connect(). We then create a cursor object, which allows us to execute SQL statements.

Next, we define a list called data that contains tuples representing the data we want to insert into the users table. Each tuple contains the first name and last name of a user.

We then use the executemany() method of the cursor to execute the batch operation. The first argument is the SQL statement, and the second argument is the data. Psycopg2 automatically handles the interpolation of values from the tuples into the SQL statement.

After executing the batch operation, we need to commit the changes using conn.commit(). Finally, we close the cursor and the connection.

Use Cases

Batch operations can be beneficial in various scenarios. Some common use cases include:

By utilizing batch operations and the executemany() method in Psycopg2, you can improve the performance and efficiency of your database operations in Python.


Psycopg2 is a powerful tool for interacting with PostgreSQL databases in Python. The executemany() method and batch operations provide an efficient way to insert or manipulate multiple rows in a single database transaction. Incorporating these techniques into your code can greatly improve performance, especially when dealing with large datasets.

So, next time you need to perform batch operations in Psycopg2, remember to leverage the executemany() method and enjoy the benefits of efficient database transactions.