21

I am trying to write a base type for PostgreSQL in C (using xcode), and I already installed PostgreSQL 11, but it seems that postgres.h cannot be simply included in the file ("'postgres.h' file not found").

Could someone tell me how to fix that problem? And can I write code under an arbitary directory, or do I have to write under the PostgreSQL directory?

Or perhaps the question should be: how to install header files like postgres.h?

3
  • 1
    Not an Xcode user, but generally you have to tell the compiler where to look for third-party headers (and then the linker where to look for the libraries themselves, which may be your next problem). stackoverflow.com/questions/14134064/…
    – Clifford
    Jun 23, 2019 at 14:21
  • 2
    If the postgres binaries are installed, pg_config is also installed (in the same bin/ directory). Running pg_config will show you the compiler- and loader-flags. (if any) Jun 23, 2019 at 16:16
  • The first question is: how did you install PostgreSQL? If from packages, then the header files are often in a different "development" package that you'd have to install. Jun 24, 2019 at 6:15

3 Answers 3

54

Install postgresql-server-dev package with this command:

sudo apt install postgresql-server-dev-XX

Replace [XX] with your already installed version of postgresql:9.5, 10, 11, 12

2
  • 2
    Recommend you actually run sudo apt-cache search postgresql-server-dev. On Kali linux these versions do not exist. I personally needed to sudo apt install postgresql-server-dev-all
    – Dave
    Jul 5, 2021 at 18:07
  • Or install any version you want with instructions found in this post: stackoverflow.com/questions/48868156/…
    – Dave
    Jul 5, 2021 at 18:41
3

You have several approaches here:

  • Search for the file yourself, using some command like

    find / -name "postgres.h" -print
    

    this will tell you (on my Mac does) the file is in:

    /usr/local/Cellar/postgresql/11.2_1/include/server/postgres.h
    

    and add the -I /usr/local/Cellar/postgresql/11.2_1/include/server option to the compiler. Check your postgresql version for the possibility of having a different one.

  • Probably there's another package for database development. Search for a package named postgresql-dev or similar, and install it. After searching packages with:

     brew search postgres 
    

    and

     brew search psql
    

    on my system doesn't appear anything that matches.

EDIT

I've checked a FreeBSD system for that file and it appears on

/usr/local/include/postgresql/server/postgres.h

So probably you have to #include <server/postgres.h> instead, and use the appropiate -I flag (as mentioned above)

2
  • I used the command 'find / -name "postgres.h" -print' and there are 2-lines useful output: /usr/local/pgsql/include/server/postgres.h /usr/local/Cellar/postgresql/11.2/include/server/postgres.h and then i used gcc -I /usr/local/Cellar/postgresql/11.2/include/server/, here is the output... clang: error: no input files... Jun 25, 2019 at 12:22
  • Then you have to learn. How can I guess the exact problem you have if you have not published any code at all? Compilation problems appear from the code... you haven't show even the source line in which the file postgres.h is #included Jun 25, 2019 at 13:41
3

I was facing the same issue while compiling postgis 3.1.7 for postgresql@13 on my Mac. The problem was that in pg_config the link to server file was

/opt/homebrew/opt/postgresql@13/include/server

While the actual server folder was in

/opt/homebrew/opt/postgresql@13/include/postgresql/server

So I moved the entire "server" folder up a directory inside "/include". And viola, postgis compiled and installed perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.