C compiler cannot find postgres.h when building a PostgreSQL extension
fatal error: 'postgres.h' file not foundShort answer
postgres.h lives in the server header directory, which is not on the compiler's default include path. Ask pg_config --includedir-server where it is and pass that with -I, or better, build with a PGXS Makefile. If several PostgreSQL versions are installed, make sure the pg_config you call belongs to the one you target.
Fix it fast
1Find out which cause you have
Print the server header directory for the PostgreSQL that pg_config belongs to, and check that postgres.h is really there.
which pg_config ls "$(pg_config --includedir-server)/postgres.h"2Match what you see, then jump to its fix
What does the error look like?
fatal error: 'postgres.h' file not found
Cause 1 of 2
The server include directory is not passed to the compiler
1Check
A plain cc command knows nothing about PostgreSQL. The server headers have their own directory, separate from the client headers that --includedir reports.
pg_config --includedir
pg_config --includedir-server2Fix
Add the server directory with -I, or use a PGXS Makefile that does this for you. In your own source, include postgres.h first.
cc -fPIC -c -I"$(pg_config --includedir-server)" foo.c
# Makefile
MODULES = foo
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)3Verify
The file compiles; with the Makefile, make and make install finish.
make && sudo make installConfirmed by postgresql.org
Cause 2 of 2
pg_config points at another PostgreSQL version or at none
1Check
By default PGXS builds for the first pg_config found in PATH. With two versions installed, that may be the wrong one.
which -a pg_config
pg_config --version2Fix
Point PG_CONFIG at the pg_config of the version you want, on the make command line. If that directory does not exist, the server headers are not present for that installation.
make PG_CONFIG=/usr/lib/postgresql/17/bin/pg_config3Verify
The version printed by that pg_config matches the server you load the extension into.
/usr/lib/postgresql/17/bin/pg_config --versionConfirmed by postgresql.org
How do I stop it coming back?
- Build extensions through PGXS so the include and library paths always come from pg_config.
- Write PG_CONFIG into your build script when more than one version is installed.
- Include postgres.h before every other header in server-side C files.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
gem install pg fails because the libpq header files are missing
Can't find the 'libpq-fe.h header
The pg gem is building from source and cannot find libpq-fe.h, the libpq C header.
3 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
DBeaver cannot download the PostgreSQL driver from Maven
The PostgreSQL driver files are missing and the download does not complete
DBeaver downloads the PostgreSQL JDBC driver from a Maven repository the first time you connect.
3 causes, each with a fixDatabases
python manage.py migrate finishes but the tables for my app are missing
relation "myapp_book" does not exist
migrate only applies migration files that exist.
3 causes, each with a fixDatabases
Django save fails on PostgreSQL: null value in a column violates a not-null constraint
django.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraint
A model field is NOT NULL in the database unless it has null=True.
3 causes, each with a fixDatabases
Django migration on PostgreSQL stops with 'pending trigger events'
django.db.utils.OperationalError: cannot ALTER TABLE "mytable" because it has pending trigger events
PostgreSQL runs a Django migration in one transaction.
2 causes, each with a fix