Skip to content

Instantly share code, notes, and snippets.

@codeagencybe
Created November 2, 2025 19:29
Show Gist options
  • Save codeagencybe/71972473e7e3f16504d55f3817fff011 to your computer and use it in GitHub Desktop.
Save codeagencybe/71972473e7e3f16504d55f3817fff011 to your computer and use it in GitHub Desktop.
Odoo connection test from odoo.conf
#!/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