Create a Python script that interacts with a PostgreSQL database to store and manage a list of tasks. Each task should have a title, description, due date, and priority level. The script should support adding, updating, deleting, and listing tasks.
PLAINFORMAT
import psycopg2
from datetime import datetime

# Database connection settings
DB_NAME = "your_database_name"
DB_USER = "your_database_user"
DB_PASSWORD = "your_database_password"
DB_HOST = "localhost"
DB_PORT = 5432

def connect():
    conn = psycopg2.connect(
        dbname=DB_NAME,
        user=DB_USER,
        password=DB_PASSWORD,
        host=DB_HOST,
        port=DB_PORT
    )
    return conn

def create_table():
    conn = connect()
    cur = conn.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS tasks (id SERIAL PRIMARY KEY, title TEXT, description TEXT, due_date DATE, priority INTEGER)")
    conn.commit()
    conn.close()

def add_task(title, description, due_date, priority):
    conn = connect()
    cur = conn.cursor()
    cur.execute("INSERT INTO tasks (title, description, due_date, priority) VALUES (%s, %s, %s, %s)", (title, description, due_date, priority))
    conn.commit()
    conn.close()

def update_task(id, title, description, due_date, priority):
    conn = connect()
    cur = conn.cursor()
    cur.execute("UPDATE tasks SET title=%s, description=%s, due_date=%s, priority=%s WHERE id=%s", (title, description, due_date, priority, id))
    conn.commit()
    conn.close()

def delete_task(id):
    conn = connect()
    cur = conn.cursor()
    cur.execute("DELETE FROM tasks WHERE id=%s", (id,))
    conn.commit()
    conn.close()

def list_tasks():
    conn = connect()
    cur = conn.cursor()
    cur.execute("SELECT * FROM tasks")
    rows = cur.fetchall()
    conn.close()
    return rows

def main():
    create_table()

    # Example usage
    add_task("Task 1", "This is task 1 description.", datetime(2022, 12, 31), 3)
    add_task("Task 2", "This is task 2 description.", datetime(2022, 11, 30), 1)

    print("Tasks before update:")
    for task in list_tasks():
        print(task)

    update_task(1, "Updated Task 1", "This is the updated task 1 description.", datetime(2022, 10, 15), 2)

    print("\nTasks after update:")
    for task in list_tasks():
        print(task)

    delete_task(2)

    print("\nTasks after deletion:")
    for task in list_tasks():
        print(task)

if __name__ == "__main__":
    main()