Insecure Design

The Principle of Least Privilege
# Connect to the database as the read-only user since we are
# not updating any information
connection = psycopg2.connect(
               dbname   = "database",
               user     = "readonly",
               password = os.getenv("DB_READONLY_USER_PASSWORD")

with connection:
  with connection.cursor() as cursor:
    cursor.execute("SELECT * FROM users WHERE email = %(email)s", dict(email=email))

    for result in cursor.fetchone():
      return result