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
libpqclient library and the optionally requested PostgreSQL server component. This target is available only if PostgreSQL is found.
結果變數¶
該模組定義了以下變數:
PostgreSQL_FOUNDBoolean indicating whether the minimum required version and components of PostgreSQL were found.
PostgreSQL_VERSION在 4.2 版被加入.
The version of PostgreSQL found.
PostgreSQL_LIBRARIESThe PostgreSQL libraries needed for linking.
PostgreSQL_INCLUDE_DIRS包含 PostgreSQL 標頭檔的引入目錄。
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:
Server在 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.
已棄用的變數¶
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()