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

在 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.

結果變數

該模組定義了以下變數:

PostgreSQL_FOUND

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

PostgreSQL_VERSION

在 4.2 版被加入.

The version of PostgreSQL found.

PostgreSQL_LIBRARIES

The PostgreSQL libraries needed for linking.

PostgreSQL_INCLUDE_DIRS

包含 PostgreSQL 標頭檔的引入目錄。

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

在 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.

已棄用的變數

The following variables are provided for backward compatibility:

PostgreSQL_VERSION_STRING

在 4.2 版之後被棄用: Superseded by the PostgreSQL_VERSION.

The version of PostgreSQL found.

範例

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