{"id":"17e2e58cffbebc2d","repo":"psycopg/psycopg2","slug":"pg-config-attr-name-failed-err","errorCode":null,"errorMessage":"pg_config --{attr_name} failed: {err}","messagePattern":"pg_config --(.+?) failed: (.+?)","errorType":"exception","errorClass":"Warning","httpStatus":null,"severity":"critical","filePath":"setup.py","lineNumber":117,"sourceCode":"<https://www.psycopg.org/docs/install.html>).\n\n\"\"\")\n            sys.exit(1)\n\n    def query(self, attr_name, *, empty_ok=False):\n        \"\"\"Spawn the pg_config executable, querying for the given config\n        name, and return the printed value, sanitized. \"\"\"\n        try:\n            pg_config_process = subprocess.run(\n                [self.pg_config_exe, \"--\" + attr_name],\n                stdout=subprocess.PIPE,\n                stderr=subprocess.PIPE)\n        except OSError:\n            raise Warning(\n                f\"Unable to find 'pg_config' file in '{self.pg_config_exe}'\")\n        if pg_config_process.returncode:\n            err = pg_config_process.stderr.decode(errors='backslashreplace')\n            raise Warning(f\"pg_config --{attr_name} failed: {err}\")\n        result = pg_config_process.stdout.decode().strip()\n        if not result and not empty_ok:\n            raise Warning(f\"pg_config --{attr_name} is empty\")\n        return result\n\n    def find_on_path(self, exename, path_directories=None):\n        if not path_directories:\n            path_directories = os.environ['PATH'].split(os.pathsep)\n        for dir_name in path_directories:\n            fullpath = os.path.join(dir_name, exename)\n            if os.path.isfile(fullpath):\n                return fullpath\n        return None\n\n    def autodetect_pg_config_path(self):\n        \"\"\"Find and return the path to the pg_config executable.\"\"\"\n        if PLATFORM_IS_WINDOWS:\n            return self.autodetect_pg_config_path_windows()","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/psycopg/psycopg2/blob/3a6d9d6ddc6b53eaa80b712f5fa6b23abbdc38db/setup.py#L99-L135","documentation":"Raised in PostgresConfig.query() (setup.py:117) as a Warning when pg_config ran but returned a non-zero exit code with stderr output. This means the executable exists but the invocation failed - typically a broken/partial PostgreSQL install, a pg_config from a different major version, an architecture mismatch (e.g. 32-bit pg_config driving a 64-bit build), or a wrapper/shim script that errors. finalize_options() catches the Warning, prints 'Error: ...', and exits 1.","triggerScenarios":"Any pg_config_helper.query('libdir'|'includedir'|'includedir-server'|'libdir' for static) during finalize_options() when that pg_config subcommand exits non-zero. The {err} field carries the decoded stderr so you can see the underlying failure.","commonSituations":"Multiple PostgreSQL installations with a stale pg_config first on PATH; corrupted/incomplete client packages; hand-written pg_config shims in custom images; cross-architecture or cross-version toolchain mismatches.","solutions":["Reinstall or repair the PostgreSQL client dev package so pg_config works cleanly.","Reproduce manually: run '/path/to/pg_config --libdir' (and --includedir) and read the stderr to find the real cause.","Reinstall to a clean PostgreSQL and use psycopg2-binary if you cannot repair the local install."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import subprocess, shutil\n\ndef pg_config_attr_works(attr: str = 'libdir', path: str | None = None) -> bool:\n    p = path or shutil.which('pg_config')\n    if not p:\n        return False\n    res = subprocess.run([p, '--' + attr], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n    return res.returncode == 0\n\nassert pg_config_attr_works('libdir') and pg_config_attr_works('includedir')","typeGuard":null,"tryCatchPattern":"# Diagnose in CI before building:\n#   pg_config --libdir && pg_config --includedir && pg_config --includedir-server\n# any non-zero exit means the install is broken - reinstall the Postgres dev package.","preventionTips":["Keep exactly one PostgreSQL dev installation first on PATH to avoid version drift.","After installing/upgrading Postgres, re-run pg_config --version and --libdir to confirm it works.","In containers, prefer official postgres:libpq images or psycopg2-binary to dodge broken pg_config."],"tags":["psycopg2","build","pg-config","install","environment"],"analyzedSha":"3a6d9d6ddc6b53eaa80b712f5fa6b23abbdc38db","analyzedAt":"2026-08-04T19:56:51.958Z","schemaVersion":2}