import os
import sys

import psycopg


def main() -> int:
    dsn = os.getenv("DATABASE_URL")
    if not dsn:
        print("DATABASE_URL missing")
        return 1

    try:
        with psycopg.connect(dsn, autocommit=True) as conn:
            with conn.cursor() as cur:
                cur.execute("SELECT 1")
                cur.fetchone()
        return 0
    except Exception as exc:
        print(exc)
        return 1


if __name__ == "__main__":
    sys.exit(main())
