FindPostgreSQL¶
Finds the PostgreSQL installation - the client library (libpq) and
optionally the server:
find_package(PostgreSQL [<version>] [...])
Imported Targets¶
This module provides the following Imported Targets:
PostgreSQL::PostgreSQLAdded in version 3.14.
Target encapsulating all usage requirements of the required
libpqclient library and the optionally requested PostgreSQL server component. This target is available only if PostgreSQL is found.
Result Variables¶
This module defines the following variables:
PostgreSQL_FOUNDBoolean indicating whether the minimum required version and components of PostgreSQL were found.
PostgreSQL_VERSIONAdded in version 4.2.
The version of PostgreSQL found.
PostgreSQL_LIBRARIESThe PostgreSQL libraries needed for linking.
PostgreSQL_INCLUDE_DIRSThe include directories containing PostgreSQL headers.
PostgreSQL_LIBRARY_DIRSThe directories containing PostgreSQL libraries.
PostgreSQL_TYPE_INCLUDE_DIRThe include directory containing PostgreSQL server headers.
Components¶
This module supports the following additional components:
ServerAdded in version 3.20.
Ensures that server headers are also found. Note that
PostgreSQL_TYPE_INCLUDE_DIRvariable is set regardless of whether this component is specified in thefind_package()call.
Deprecated Variables¶
The following variables are provided for backward compatibility:
PostgreSQL_VERSION_STRINGDeprecated since version 4.2: Superseded by the
PostgreSQL_VERSION.The version of PostgreSQL found.
Examples¶
Finding the PostgreSQL client library and linking it to a project target:
find_package(PostgreSQL)
target_link_libraries(project_target PRIVATE PostgreSQL::PostgreSQL)
Specifying a minimum required PostgreSQL version:
find_package(PostgreSQL 11)
Finding the PostgreSQL client library and requiring server headers using the
Server component provides an imported target with all usage requirements,
which can then be linked to a project target:
find_package(PostgreSQL COMPONENTS Server)
target_link_libraries(project_target PRIVATE PostgreSQL::PostgreSQL)
When checking for PostgreSQL client library features, some capabilities are
indicated by preprocessor macros in the libpq-fe.h header (e.g.
LIBPQ_HAS_PIPELINING). Others may require using the
check_symbol_exists() command:
find_package(PostgreSQL)
target_link_libraries(project_target PRIVATE PostgreSQL::PostgreSQL)
# The PQservice() function is available as of PostgreSQL 18.
if(TARGET PostgreSQL::PostgreSQL)
include(CheckSymbolExists)
include(CMakePushCheckState)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES PostgreSQL::PostgreSQL)
check_symbol_exists(PQservice "libpq-fe.h" PROJECT_HAS_PQSERVICE)
cmake_pop_check_state()
endif()