{"id":"2463f02dd29c609f","repo":"psycopg/psycopg2","slug":"you-can-t-specify-both-database-and-dbname-arg","errorCode":null,"errorMessage":"you can't specify both 'database' and 'dbname' arguments","messagePattern":"you can't specify both 'database' and 'dbname' arguments","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/extensions.py","lineNumber":151,"sourceCode":"\n    def getquoted(self, _null=b\"NULL\"):\n        return _null\n\n\ndef make_dsn(dsn=None, **kwargs):\n    \"\"\"Convert a set of keywords into a connection strings.\"\"\"\n    if dsn is None and not kwargs:\n        return ''\n\n    # If no kwarg is specified don't mung the dsn, but verify it\n    if not kwargs:\n        parse_dsn(dsn)\n        return dsn\n\n    # Override the dsn with the parameters\n    if 'database' in kwargs:\n        if 'dbname' in kwargs:\n            raise TypeError(\n                \"you can't specify both 'database' and 'dbname' arguments\")\n        kwargs['dbname'] = kwargs.pop('database')\n\n    # Drop the None arguments\n    kwargs = {k: v for (k, v) in kwargs.items() if v is not None}\n\n    if dsn is not None:\n        tmp = parse_dsn(dsn)\n        tmp.update(kwargs)\n        kwargs = tmp\n\n    dsn = \" \".join([\"{}={}\".format(k, _param_escape(str(v)))\n        for (k, v) in kwargs.items()])\n\n    # verify that the returned dsn is valid\n    parse_dsn(dsn)\n\n    return dsn","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/psycopg/psycopg2/blob/3a6d9d6ddc6b53eaa80b712f5fa6b23abbdc38db/lib/extensions.py#L133-L169","documentation":"Raised by make_dsn() (lib/extensions.py:150-152) when both 'database' and 'dbname' are present in the keyword arguments. These are aliases for the same PostgreSQL connection parameter (libpq uses 'dbname'); psycopg2 accepts 'database' for familiarity but forbids specifying both to avoid ambiguity about which value wins.","triggerScenarios":"Calling psycopg2.connect() or make_dsn() with both database=... and dbname=... in the same call, e.g. connect(dbname='x', database='y'), or passing a kwargs dict that merges two config sources each using a different alias.","commonSituations":"Merging a base DSN/kwargs (using 'dbname') with overrides (using 'database') from environment variables or a config file. Common when a framework uses 'database' (SQLAlchemy/ Django style) while the ops layer uses 'dbname' (libpq style).","solutions":["Standardize on a single key: use 'dbname' everywhere (libpq native) or 'database' everywhere.","When merging configs, drop one alias before calling connect/make_dsn: kwargs.pop('database', None).","If using make_dsn(dsn, **kwargs), ensure the DSN string and kwargs don't each contribute a different alias."],"exampleFix":"// before\nconn = psycopg2.connect(host=h, dbname='prod', database='prod')\n// after\nconn = psycopg2.connect(host=h, dbname='prod')","handlingStrategy":"validation","validationCode":"if 'database' in kwargs and 'dbname' in kwargs:\n    raise TypeError('specify only one of database/dbname')\nif 'database' in kwargs:\n    kwargs['dbname'] = kwargs.pop('database')","typeGuard":"def has_duplicate_db_key(kwargs: dict) -> bool:\n    return 'database' in kwargs and 'dbname' in kwargs","tryCatchPattern":"try:\n    conn = psycopg2.connect(**kwargs)\nexcept TypeError as e:\n    if 'database' in str(e) and 'dbname' in str(e):\n        kwargs.pop('database', None)\n        conn = psycopg2.connect(**kwargs)\n    else: raise","preventionTips":["Standardize the whole codebase on one alias (prefer 'dbname').","When merging configs from different layers, normalize to a single key."],"tags":["dsn","connection","config","type-error","alias-conflict"],"analyzedSha":"3a6d9d6ddc6b53eaa80b712f5fa6b23abbdc38db","analyzedAt":"2026-08-04T19:56:51.958Z","schemaVersion":2}