Announcing autoscaling in feature-preview!Learn More
Guides

Connect a Python application to Neon using Pyscopg

Set up a Neon project in seconds and connect from a Python application using Pyscopg

This guide describes how to create a Neon project and connect to it from a simple Python application using Psycopg (psycopg2), a popular PostgreSQL database adapter for the Python programming language. The application connects to Neon and retrieves the current time and PostgreSQL version.

To connect:

  1. Create a Neon Project
  2. Create a Python project
  3. Store your Neon credentials
  4. Configure your Python script
  5. Test your connection

Create a Neon project

If you do not have one already, create a Neon project.

  1. Navigate to the Projects page in the Neon Console.
  2. Click New Project.
  3. Specify your project settings and click Create Project.

The project is created with a default neondb database, which you will connect to.

Create a Python project

  1. Create a project directory and change to the newly created directory.

    mkdir neon-python-example
    cd neon-python-example
  2. Set up a Python virtual environment in this directory. The virtual environment isolates your project's Python environment (including installed packages) from the rest of your system.

    python3 -m venv env
  3. Activate the virtual environment. When the virtual environment is activated, Python uses the environment's version of Python and any installed packages.

    source env/bin/activate
  4. Install psycopg2 and python-dotenv in your project's root directory. You can install them using pip:

    pip install psycopg2-binary python-dotenv

Store your Neon credentials

Add a .env file to your project's root directory and add your Neon connection string to it. You will find the connection string for your database in the Connection Details widget on the Neon Dashboard. For more information, see Connect from any application.

Your connection string will look something like this:

DATABASE_URL=postgres://<user>:<password>@ep-snowy-unit-550577.us-east-2.aws.neon.tech/neondb

Configure your python script

Add a neon-connect.py file to your project's root directory and add the following code. The script connects to your Neon database and retrieves the current time and PostgreSQL version.

import os
import psycopg2
from dotenv import load_dotenv

# Load .env file
load_dotenv()

# Get the connection string from the environment variable
connection_string = os.getenv('DATABASE_URL')

# Connect to the PostgreSQL database
conn = psycopg2.connect(connection_string)

# Create a cursor object
cur = conn.cursor()

# Execute SQL commands to retrieve the current time and version from PostgreSQL
cur.execute('SELECT NOW();')
time = cur.fetchone()[0]

cur.execute('SELECT version();')
version = cur.fetchone()[0]

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

# Print the results
print('Current time:', time)
print('PostgreSQL version:', version)

Test your connection

Run the neon-connect.py script to test your connection.

python3 neon-connect.py

If the connection is successful, the script returns information similar to the following:

Current time: 2023-05-24 08:53:10.403140+00:00
PostgreSQL version: PostgreSQL 15.2 on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

Need help?

Send a request to support@neon.tech, or join the Neon community forum.

Edit this page
Was this page helpful?