23

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?

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Jasper Zhou
  • 449
  • 1
  • 5
  • 13
  • 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). https://stackoverflow.com/questions/14134064/how-to-set-include-path-in-xcode-project – Clifford Jun 23 '19 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) – wildplasser Jun 23 '19 at 16:16
  • 1
    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. – Laurenz Albe Jun 24 '19 at 06:15

3 Answers3

57

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

Kerem
  • 11,377
  • 5
  • 59
  • 58
Nuzhat Ansari
  • 581
  • 5
  • 6
  • 3
    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 05 '21 at 18:07
  • Or install any version you want with instructions found in this post: https://stackoverflow.com/questions/48868156/how-to-install-the-specific-version-of-postgres – Dave Jul 05 '21 at 18:41
4

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)

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
  • 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... – Jasper Zhou Jun 25 '19 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 `#include`d – Luis Colorado Jun 25 '19 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.