0

I'm trying to create my own function (using C) in postgres for which I'm referring this doc: http://www.linuxgazette.cz/139/peterson.html

For compiling - I'm referring: Section 33.9.6 . But, when I run below command:

   cc -fpic -c example.c

I get this error:

   example.c:1:22: fatal error: postgres.h: No such file or directory
 #include "postgres.h"
                  ^
compilation terminated.

I have also installed postgresql-server-dev-all package and tried with the solution provided in this link: How to fix 'postgres.h' file not found problem?

Nothing working for me. Please let me know how I can have postgres.h file. I'm using postgres-12 on centos -7.

2
  • The Section 33.9.6 you refer to is for version 8.2 which is well past EOL. The current section is 37.10.5. It has: cc -fPIC -c foo.c. I'm not a C programmer, so I'm not sure what the change to PIC means. Commented Sep 6, 2020 at 17:47
  • Thanks for the advice. But, even after changing the casing -it's not working for me. Commented Sep 13, 2020 at 19:59

2 Answers 2

2

for ubuntu: apt-get -y install postgresql-server-dev-13 after apt-get -y install postgresql-13.

Or more generally:

apt-get -y install postgresql-xx
apt-get -y install postgresql-server-dev-xx

where xx is the target PostgreSQL version.

Sign up to request clarification or add additional context in comments.

Comments

1

First: If you didn't install PostgreSQL from source, make sure that you installed the headers, which are in the -devel package.

If the problem persists even with the headers installed, the reason is that you forgot to tell the compiler where to look for header files:

cc -fpic -I /location/of/postgres/include -c example.c

To find that location, you can use pg_config:

pg_config --includedir

There is a similar --libdir option to show the library path for when you link the executable.

4 Comments

Thank you so much for providing me this useful info. For me this path is: /usr/include. I ran the compilation command by the first command you provided with header file path and again I got the same error. However, when I went to this path : /usr/include. and search for the header file for postgres.h - I didn't get any. I see many other .h header files there. Can there be any problem with the package I'm working with?
If you are using the PGDG packages, that path would be /usr/pgsql-12/include. You never said what packages you are using. It is also possible that you have several installations of PostgreSQL. Without knowing details, it is hard to give more concrete advice.
Sorry, but could you please help me to identify what package I'm using. I know how to check version and the version is 12.4
That would be rpm -qa | grep postgres. But see my updated answer; perhaps you simply forgot to install the headers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.