{"id":"e9877fb11ac819f1","repo":"psycopg/psycopg2","slug":"unable-to-find-pg-config-file-in-self-pg-confi","errorCode":null,"errorMessage":"Unable to find 'pg_config' file in '{self.pg_config_exe}'","messagePattern":"Unable to find 'pg_config' file in '(.+?)'","errorType":"exception","errorClass":"Warning","httpStatus":null,"severity":"critical","filePath":"setup.py","lineNumber":113,"sourceCode":"If you prefer to avoid building psycopg2 from source, please install the PyPI\n'psycopg2-binary' package instead.\n\nFor further information please check the 'doc/src/install.rst' file (also at\n<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","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/psycopg/psycopg2/blob/3a6d9d6ddc6b53eaa80b712f5fa6b23abbdc38db/setup.py#L95-L131","documentation":"Raised in PostgresConfig.query() (setup.py:113) as a Warning when subprocess.run([pg_config_exe, '--attr']) raises OSError - the resolved pg_config path does not exist, is not executable, or cannot be spawned. psycopg2's source build needs pg_config to discover include/library directories; finalize_options() catches this Warning, prints 'Error: ...', and calls sys.exit(1), aborting the install. The path was obtained from --pg-config, setup.cfg, PATH autodetect, or (Windows) the registry.","triggerScenarios":"pip install psycopg2 (source build) on a host where the resolved pg_config path is wrong/missing. Triggers: bare/slim base images without PostgreSQL dev headers, a stale --pg-config value, a broken PATH, or a 32/64-bit mismatch where the registry pointed pg_config.exe at the wrong bitness.","commonSituations":"Alpine/slim Debian/Ubuntu containers without libpq-dev; macOS without Postgres.app or libpq in PATH; CI images lacking postgresql-server-dev-*; Windows installs where the registry Base Directory is gone.","solutions":["Install the PostgreSQL dev package: Debian/Ubuntu 'apt-get install libpq-dev', Alpine 'apk add postgresql-dev', RHEL/Fedora 'dnf install libpq-devel', macOS 'brew install libpq' (and link) or install Postgres.app.","Skip the source build entirely: pip install psycopg2-binary (recommended for app deployment).","Point pip at the right binary: pip install psycopg2 --global-option build_ext --global-option --pg-config=/path/to/pg_config, or set pg_config in setup.cfg."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import os, shutil\n\ndef pg_config_is_runnable(path: str | None = None) -> bool:\n    p = path or shutil.which('pg_config')\n    return bool(p) and os.path.isfile(p) and os.access(p, os.X_OK)\n\n# fail fast before pip install:\nassert pg_config_is_runnable(), 'pg_config missing - install libpq-dev / postgresql-dev or use psycopg2-binary'","typeGuard":null,"tryCatchPattern":"# Pre-flight in your Dockerfile / CI before installing psycopg2:\n#   command -v pg_config >/dev/null || { apt-get update && apt-get install -y libpq-dev; }\n#   pg_config --version","preventionTips":["In Dockerfiles, install libpq-dev (Debian/Ubuntu), postgresql-dev (Alpine), or libpq-devel (RHEL) before pip install psycopg2.","For application deployment, depend on psycopg2-binary to avoid source builds entirely.","Pin pg_config explicitly with --pg-config or setup.cfg rather than relying on PATH."],"tags":["psycopg2","build","pg-config","install","environment","docker"],"analyzedSha":"3a6d9d6ddc6b53eaa80b712f5fa6b23abbdc38db","analyzedAt":"2026-08-04T19:56:51.958Z","schemaVersion":2}