Ilia Choly
Ilia Choly

Reputation: 18557

FindPostgreSQL.cmake won't work on ubuntu

I'm trying to get the FindPostgreSQL module to find /usr/include/postgresql/libpq-fe.h.

Here's what I have in my CMakeLists.txt:

find_package(PostgreSQL REQUIRED)

This is the error I get:

CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (MESSAGE):
  Could NOT find PostgreSQL (missing: PostgreSQL_TYPE_INCLUDE_DIR) (found
  version "9.2.2")
Call Stack (most recent call first):
  /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:288 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-2.8/Modules/FindPostgreSQL.cmake:155 (find_package_handle_standard_args)
  CMakeLists.txt:7 (find_package)

I added the following lines before calling find_package but it didn't seem to have any effect.

set(PostgreSQL_ADDITIONAL_VERSIONS "9.2.2")
set(PostgreSQL_ADDITIONAL_SEARCH_PATHS ${PostgreSQL_ADDITIONAL_SEARCH_PATHS} "/usr/include/postgresql")

I also tried googling for PostgreSQL_TYPE_INCLUDE_DIR but didn't find anything. What else can I try?

Upvotes: 17

Views: 22485

Answers (5)

Vladlen
Vladlen

Reputation: 138

Package managers on different systems use own locations to install PostgreSQL. CMakePostgreSQL.cmake has the list of locations, but it cannot include everything. Instead it has the description on lines 80-90 how to solve this issue:

# If you have installed PostgreSQL in a non-standard location.
# (Please note that in the following comments, it is assumed that <Your Path>
# points to the root directory of the include directory of PostgreSQL.)
# Then you have three options.
# 1) After CMake runs, set PostgreSQL_INCLUDE_DIR to <Your Path>/include and
#    PostgreSQL_LIBRARY_DIR to wherever the library pq (or libpq in windows) is
# 2) Use CMAKE_INCLUDE_PATH to set a path to <Your Path>/PostgreSQL<-version>. This will allow find_path()
#    to locate PostgreSQL_INCLUDE_DIR by utilizing the PATH_SUFFIXES option. e.g. In your CMakeLists.txt file
#    set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "<Your Path>/include")
# 3) Set an environment variable called ${PostgreSQL_ROOT} that points to the root of where you have
#    installed PostgreSQL, e.g. <Your Path>.

In my case it resolves all problems by setting PostgreSQL_ROOT environment variable:

export PostgreSQL_ROOT=/usr/local/pgsql
cmake ..

Just point to your PostgreSQL root (directory where bin, include,lib are located), and it should work.

Upvotes: 0

Martin Gerhardy
Martin Gerhardy

Reputation: 1990

On Ubuntu you can also work around that issue by calling cmake with having PostgreSQL_TYPE_INCLUDE_DIR defined like this:

cmake -DPostgreSQL_TYPE_INCLUDE_DIR=/usr/include/postgresql/

See the bug report [1] for this issue and a potential fix [2]. Álso see the discussion about the reasoning behind the move on the debian mailinglist at [3].

On Ubuntu/Debian, starting with PostgreSQL 9.3 the header file pg_type.h is moved to a separate package (from libpq-dev to postgresql-server-dev) and consequently the file pg_type.h is moved to a new location

Upvotes: 8

Tombart
Tombart

Reputation: 32378

Make sure you've installed both libpq-dev\ and postgresql-server-dev-all (or specific version e.g. postgresql-server-dev-9.4)

$ dpkg --get-selections | grep -e "libpq-dev\|postgresql-server-dev"

in case you're missing some package

apt-get install libpq-dev postgresql-server-dev-all

should fix it.

Upvotes: 40

Max Barraclough
Max Barraclough

Reputation: 264

From Linux Mint 17.3 ("Rosa") with PostgreSQL 9.3, I had to adjust ilia choly's solution (interestingly, the suggested postgres entry in the list was already present in the file, but wasn't enough to fix things).

I had to edit /usr/share/cmake-2.8/Modules/FindPostgreSQL.cmake around line 114 and add postgresql/9.3 so that the find_path call looks like

find_path(PostgreSQL_TYPE_INCLUDE_DIR
  NAMES catalog/pg_type.h
  PATHS
   # Look in other places.
   ${PostgreSQL_ROOT_DIRECTORIES}
  PATH_SUFFIXES
    postgresql/9.3
    postgresql
    pgsql/server
    postgresql/server
    include/server
  # Help the user find it if we cannot.
  DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
)

Upvotes: 2

Ilia Choly
Ilia Choly

Reputation: 18557

After a bit more debugging I figured out that it's getting stuck trying to find pg_type.h

This file is located in /usr/include/postgresql/catalog/pg_types.h but the module is expecting to find it in /usr/include/postgresql/server/catalog/pg_types.h

find_path(PostgreSQL_TYPE_INCLUDE_DIR
  NAMES catalog/pg_type.h
  PATHS
   # Look in other places.
   ${PostgreSQL_ROOT_DIRECTORIES}
  PATH_SUFFIXES
    pgsql/server
    postgresql/server
    include/server
  # Help the user find it if we cannot.
  DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
)

It works if I add postgresql to the PATH_SUFFIXES

find_path(PostgreSQL_TYPE_INCLUDE_DIR
  NAMES catalog/pg_type.h
  PATHS
   # Look in other places.
   ${PostgreSQL_ROOT_DIRECTORIES}
  PATH_SUFFIXES
    postgresql
    pgsql/server
    postgresql/server
    include/server
  # Help the user find it if we cannot.
  DOC "The ${PostgreSQL_INCLUDE_DIR_MESSAGE}"
)

Upvotes: 7

Related Questions