Neon is Generally Available! Serverless Postgres with branching to boost your development velocity.Read more
Guides/Languages/Python

Connect a Python application to Neon using Psycopg

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

This guide describes how to create a Neon project and connect to it from a simple Python application using Psycopg (psycopg2), a popular Postgres database adapter for the Python programming language. The application connects to Neon and retrieves the current time and Postgres 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 ready-to-use 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 can find all of the connection details listed above 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]@[neon_hostname]/[dbname]?sslmode=require

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 Postgres 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 Postgres 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?

Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more detail, see Getting Support.

Last updated on

Edit this page
Was this page helpful?