Created
November 2, 2025 19:29
-
-
Save codeagencybe/71972473e7e3f16504d55f3817fff011 to your computer and use it in GitHub Desktop.
Odoo connection test from odoo.conf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import psycopg2 | |
| import sys | |
| import configparser | |
| # Read odoo.conf | |
| config = configparser.ConfigParser() | |
| config.read('/etc/odoo/odoo.conf') | |
| # Get database connection details from config | |
| DB_HOST = config.get('options', 'db_host', fallback='localhost') | |
| DB_PORT = config.getint('options', 'db_port', fallback=5432) | |
| DB_USER = config.get('options', 'db_user', fallback='odoo') | |
| DB_PASSWORD = config.get('options', 'db_password', fallback='') | |
| # If no password in config, prompt | |
| if not DB_PASSWORD: | |
| DB_PASSWORD = input("Enter PostgreSQL password: ") | |
| print(f"π Testing connection to {DB_HOST}:{DB_PORT} as {DB_USER}...") | |
| try: | |
| # Attempt connection | |
| conn = psycopg2.connect( | |
| host=DB_HOST, | |
| port=DB_PORT, | |
| user=DB_USER, | |
| password=DB_PASSWORD, | |
| dbname="postgres", | |
| connect_timeout=5 | |
| ) | |
| # Test query | |
| cursor = conn.cursor() | |
| cursor.execute("SELECT version();") | |
| version = cursor.fetchone()[0] | |
| # List databases | |
| cursor.execute("SELECT datname FROM pg_database WHERE datistemplate = false;") | |
| databases = [row[0] for row in cursor.fetchall()] | |
| print("β Connection successful!") | |
| print(f"π PostgreSQL version: {version[:80]}...") | |
| print(f"ποΈ Available databases ({len(databases)}): {', '.join(databases[:5])}") | |
| if len(databases) > 5: | |
| print(f" ... and {len(databases) - 5} more") | |
| cursor.close() | |
| conn.close() | |
| sys.exit(0) | |
| except psycopg2.OperationalError as e: | |
| print(f"β Connection failed: {e}") | |
| sys.exit(1) | |
| except Exception as e: | |
| print(f"β Error: {e}") | |
| sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment