{"library":"psycopg-pool","code":"import os\nfrom psycopg_pool import ConnectionPool\n\n# Get connection string from environment variable for security\nDB_URL = os.environ.get('DATABASE_URL', 'postgresql://user:password@host:port/dbname')\n\n# Create a connection pool as a context manager\n# The pool is opened and closed automatically\nwith ConnectionPool(DB_URL, min_size=1, max_size=5) as pool:\n    print(\"Connection pool created.\")\n\n    # Get a connection from the pool as a context manager\n    with pool.connection() as conn:\n        # The connection context handles transaction commit/rollback\n        # and returns the connection to the pool.\n        with conn.cursor() as cur:\n            cur.execute(\"SELECT 1 + 1\")\n            result = cur.fetchone()\n            print(f\"Result: {result[0]}\")\n\n    # Example of getting multiple connections (will block if pool exhausted)\n    print(\"Attempting to get another connection...\")\n    with pool.connection() as conn2:\n        with conn2.cursor() as cur2:\n            cur2.execute(\"SELECT 'Hello from conn2'\")\n            result2 = cur2.fetchone()\n            print(f\"Result from conn2: {result2[0]}\")\n\nprint(\"Pool is closed after exiting the 'with' block.\")","lang":"python","description":"This quickstart demonstrates how to initialize a `ConnectionPool` using a connection string, obtain a connection using a context manager, execute a simple query, and ensure proper connection handling (returning to pool, transaction management). It also highlights how the pool manages connections for multiple requests.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}