keepcalculator

06 Numbers and Powers

PostgreSQL Python Connect: How to Use

Card No. 06·48002 · Updated · Standard formula, shown below

To connect to a PostgreSQL database from Python, you use the psycopg2 library (or the async asyncpg), which provides a connect() function that takes parameters like host, port, database, user, and password, and returns a connection object. From there you create a cursor, execute SQL, and commit or fetch results. This article explains the full process, common pitfalls, and how to choose the right driver.

Background: Why psycopg2 Is the Standard

PostgreSQL speaks its own wire protocol, so Python needs a driver that translates between Python objects and that protocol. psycopg2 has been the de facto standard since the early 2000s because it is mature, actively maintained, and supports most PostgreSQL features, including server-side cursors, COPY, and asynchronous notifications. It is a C extension, so it is fast, but that also means you need to install the PostgreSQL client libraries on your system (on most Linux distros, the package libpq-dev or libpq is required).

Other options exist: asyncpg is a pure-Python async driver that is very fast for concurrent workloads, pg8000 is a pure-Python sync driver with no C dependencies, and psycopg (version 3) is the modern successor to psycopg2, supporting both sync and async. For most new projects, psycopg3 is a good choice, but psycopg2 remains the most widely documented and deployed.

How the Connection Works

The psycopg2.connect() function opens a network connection to the PostgreSQL server. The basic parameters are:

  • host – the server address (e.g., localhost or an IP). Default is the Unix socket.
  • port – default is 5432.
  • dbname (or database) – the name of the database.
  • user – the database role.
  • password – the password for that role.

You can also use a connection string: postgresql://user:password@host:port/dbname. The connection object manages a transaction; you must call commit() to make changes permanent, or rollback() to discard them. When you are done, close the connection to free resources.

Step-by-Step: Basic Connection and Query

Here is the minimal pattern you will use in almost every script:

  1. Install psycopg2: pip install psycopg2-binary (the binary version bundles the needed libraries).
  2. Import: import psycopg2.
  3. Connect: conn = psycopg2.connect(host="localhost", dbname="mydb", user="postgres", password="secret").
  4. Create a cursor: cur = conn.cursor().
  5. Execute SQL: cur.execute("SELECT * FROM users").
  6. Fetch results: rows = cur.fetchall().
  7. Commit if you made changes: conn.commit().
  8. Close cursor and connection: cur.close() and conn.close().

Always use parameterized queries to avoid SQL injection: cur.execute("INSERT INTO users (name) VALUES (%s)", (name,)). Never concatenate strings into SQL.

Common Problems and How to Fix Them

Most connection issues fall into a few categories:

  • ModuleNotFoundError: No module named ‘psycopg2’ – You haven’t installed it, or you installed psycopg2-binary but the import name is still psycopg2. Run pip install psycopg2-binary.
  • connection refused – The server is not running, the host/port is wrong, or a firewall is blocking. Check pg_isready on the server.
  • password authentication failed – The user or password is incorrect, or the server’s pg_hba.conf does not allow password auth for that host.
  • database “mydb” does not exist – You need to create the database first with CREATE DATABASE.
  • SSL errors – If the server requires SSL, add sslmode=require to the connection string.

If you are using a Unix socket, the host parameter should be the directory of the socket (often /var/run/postgresql), not localhost.

How to Choose a Driver

Your choice depends on your project’s needs:

  • psycopg2 – Best for traditional synchronous scripts, Django, and most tutorials. Stable, feature-rich.
  • psycopg (v3) – Modern, supports both sync and async, better typing. Use for new projects if you don’t need legacy compatibility.
  • asyncpg – Pure Python, very fast for high-concurrency async applications (e.g., FastAPI, asyncio). Does not support all PostgreSQL features like COPY FROM STDIN in the same way.
  • pg8000 – Pure Python, no C dependencies, useful when you can’t compile extensions.

For most simple scripts and web apps, psycopg2-binary is the easiest to install and the most documented. If you are building an async service, asyncpg is a strong choice.

Connection Pools and Best Practices

Opening a new connection for every query is slow. For applications, use a connection pool. psycopg2.pool provides SimpleConnectionPool and ThreadedConnectionPool. Example:

from psycopg2.pool import SimpleConnectionPool
pool = SimpleConnectionPool(1, 10, host="localhost", dbname="mydb", user="postgres", password="secret")
conn = pool.getconn()
try:
    cur = conn.cursor()
    cur.execute("SELECT 1")
    cur.fetchone()
finally:
    pool.putconn(conn)

Always use try/finally to return connections to the pool. For async, asyncpg has a built-in pool: asyncpg.create_pool(). Also, set a reasonable connect_timeout (e.g., 10 seconds) to avoid hanging.

Security and Safety Considerations

Never hardcode credentials in your source code. Use environment variables or a config file that is not committed to version control. For example:

import os
conn = psycopg2.connect(host=os.getenv("DB_HOST"), dbname=os.getenv("DB_NAME"), user=os.getenv("DB_USER"), password=os.getenv("DB_PASS"))

Use SSL when connecting over untrusted networks: add sslmode=verify-full with the appropriate CA certificate. Also, always use parameterized queries to prevent SQL injection. Never use cur.execute("SELECT * FROM " + table) with user input; if you must dynamically choose a table name, validate it against an allowlist.

Frequently Asked Questions

How do I install psycopg2 on Windows?

The easiest way is pip install psycopg2-binary. This installs a precompiled wheel, so you don’t need to have PostgreSQL installed locally. If you need the source version, you must have Visual C++ build tools and the PostgreSQL development libraries.

What is the difference between psycopg2 and psycopg2-binary?

psycopg2 is the source package that compiles against libpq on your system. psycopg2-binary is a precompiled version that bundles libpq, so it installs faster and works without a local PostgreSQL installation. For development, binary is fine; for production, some prefer the source package to link against a specific libpq version, but binary is generally acceptable.

Why is my connection hanging?

This usually means the server is not reachable (firewall, wrong host) or the server is not responding. Set a connect_timeout in the connection parameters, e.g., connect_timeout=5. Also check that the server is listening on the expected port with ss -tlnp | grep 5432 on the server.

Can I use the same connection from multiple threads?

psycopg2 connections are not thread-safe by default. Use a separate connection per thread, or use a ThreadedConnectionPool that hands out different connections to each thread. If you must share a connection, you need to use a lock, but that serializes access.

How do I handle transactions with psycopg2?

By default, psycopg2 starts a transaction implicitly. You must call conn.commit() to save changes, or conn.rollback() to discard. If you want autocommit, set conn.autocommit = True before executing. For context managers, you can use with conn: which commits on success and rolls back on exception.

What is the best way to load a CSV file into PostgreSQL from Python?

Use the COPY command via cur.copy_expert(). For example: cur.copy_expert("COPY mytable FROM STDIN WITH CSV HEADER", file). This is much faster than inserting row by row. Make sure the file is open in text mode.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →