FindPostgreSQL — CMake 4.2.6 Documentation

# 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::PostgreSQL`

  Added in version 3.14.

  Target encapsulating all usage requirements of the required `libpq` client 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_FOUND`

  Boolean indicating whether the minimum required version and components of PostgreSQL were found.

`PostgreSQL_VERSION`

  Added in version 4.2.

  The version of PostgreSQL found.

`PostgreSQL_LIBRARIES`

  The PostgreSQL libraries needed for linking.

`PostgreSQL_INCLUDE_DIRS`

  The include directories containing PostgreSQL headers.

`PostgreSQL_LIBRARY_DIRS`

  The directories containing PostgreSQL libraries.

`PostgreSQL_TYPE_INCLUDE_DIR`

  The include directory containing PostgreSQL server headers.

## Components

This module supports the following additional components:

`Server`

  Added in version 3.20.

  Ensures that server headers are also found. Note that `PostgreSQL_TYPE_INCLUDE_DIR` variable is set regardless of whether this component is specified in the `find_package()` call.

## Deprecated Variables

The following variables are provided for backward compatibility:

`PostgreSQL_VERSION_STRING`

  Deprecated 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()