[파이썬] Psycopg2에서 Django ORM 및 Psycopg2

Introduction to Psycopg2

Psycopg2 is a PostgreSQL adapter for Python. It allows Python programs to connect to PostgreSQL databases, execute SQL statements, and interact with the database using Python code.

In this article, we will explore how to use Psycopg2 with the Django ORM and some useful features of Psycopg2 in Python.

Connecting to PostgreSQL with Psycopg2

To connect to a PostgreSQL database using Psycopg2, you need to provide the hostname, port number, database name, username, and password. Here is an example of how to establish a connection:

import psycopg2

conn = psycopg2.connect(
    host="localhost",
    port=5432,
    database="mydatabase",
    user="myuser",
    password="mypassword"
)

cursor = conn.cursor()

# Perform database operations

cursor.close()
conn.close()

Using Psycopg2 with Django ORM

Django is a popular web framework that includes its own ORM (Object-Relational Mapping) for interacting with the database. Psycopg2 can be used as the underlying database adapter for Django.

To configure Django to use Psycopg2, you need to install the psycopg2 package and update the DATABASES setting in your Django project’s settings.py file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Once configured, you can use Django’s ORM to perform database operations in your Django models:

from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    ...

Django will automatically generate the necessary SQL statements and use Psycopg2 to execute them against the database.

Useful Features of Psycopg2

Psycopg2 provides several useful features that can enhance your database operations:

Batch Execution

Psycopg2 supports batch execution of SQL statements, allowing you to execute multiple statements in a single transaction for improved performance. Here’s an example of batch execution:

with conn.cursor() as cursor:
    records = [
        ('John', 'Doe'),
        ('Jane', 'Smith'),
        ...
    ]
    insert_query = "INSERT INTO mytable (first_name, last_name) VALUES (%s, %s)"
    cursor.executemany(insert_query, records)
    conn.commit()

Asynchronous Queries

Psycopg2 provides support for asynchronous queries, allowing you to execute queries in a non-blocking manner. This is particularly useful for applications that require high concurrency and responsiveness. Here’s an example of how to use asynchronous queries:

import asyncio
import asyncpg

async def execute_async():
    conn = await asyncpg.connect(
        host="localhost",
        port=5432,
        database="mydatabase",
        user="myuser",
        password="mypassword"
    )
    result = await conn.fetch("SELECT * FROM mytable")
    await conn.close()
    return result

asyncio.run(execute_async())

Connection Pooling

Connection pooling allows you to reuse existing database connections instead of creating new ones for each user request. Psycopg2 provides support for connection pooling through the psycopg2.pool module. Here’s an example of setting up a connection pool:

import psycopg2.pool

pool = psycopg2.pool.SimpleConnectionPool(
    minconn=1,
    maxconn=10,
    host="localhost",
    port=5432,
    database="mydatabase",
    user="myuser",
    password="mypassword"
)

conn = pool.getconn()
with conn.cursor() as cursor:
    cursor.execute("SELECT * FROM mytable")
    result = cursor.fetchall()
pool.putconn(conn)

Conclusion

Psycopg2 is a powerful PostgreSQL adapter for Python that enables seamless integration with the Django ORM and provides useful features for efficient database operations. By leveraging the capabilities of Psycopg2, you can build robust and performant applications that interact with PostgreSQL databases.