You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Drogon框架连接PostgreSQL(Neon)报错:不支持PostgreSQL

解决Drogon框架提示PostgreSQL不支持的问题

我基于C++的Drogon框架搭配Neon Database(PostgreSQL)开发项目,用CMake做项目配置。运行时收到错误:

"The PostgreSQL is not supported by drogon, please install the development library first"

已确认系统安装了PostgreSQL,重装过PostgreSQL开发库,CLI能正常访问数据库,但问题依旧。相关代码和CMake配置如下:

C++代码片段

drogon::app().createDbClient("postgresql", // type
                             "[neon url...]",  // host
                              5432,         // port
                             "[dbname]",     // dbname
                             "users_owner",       // user
                             "[password-placeholder]",   // passwd
                             5);           // connNum

CMake配置

# use cmake -S . -B build
cmake_minimum_required(VERSION 3.27)
project(PatrolSystem)

set(CMAKE_CXX_STANDARD 20)

set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")

find_package(PostgreSQL REQUIRED)
find_package(Drogon REQUIRED)

add_executable(untitled main.cpp
        # ... other source files ...
)

# Link the necessary libraries
target_link_libraries(untitled PostgreSQL::PostgreSQL Drogon::Drogon)

解决步骤

1. 确认Drogon编译时已开启PostgreSQL支持

Drogon默认不会自动开启PostgreSQL支持,必须在编译时显式指定:

  • 若通过包管理器安装Drogon,检查是否有drogon-postgresql这类扩展包,未安装则补上;
  • 若源码编译Drogon,重新编译时添加参数:
cmake -DENABLE_POSTGRESQL=ON ..
make install

2. 验证CMake是否正确找到PostgreSQL

在构建目录查看CMake输出日志,确认find_package(PostgreSQL REQUIRED)是否成功。也可以在CMakeLists.txt中添加调试输出:

message(STATUS "PostgreSQL include dir: ${PostgreSQL_INCLUDE_DIRS}")
message(STATUS "PostgreSQL libraries: ${PostgreSQL_LIBRARIES}")

运行cmake -S . -B build后查看输出,若路径为空或错误,手动指定PostgreSQL安装路径:

set(PostgreSQL_ROOT "/usr/local/pgsql") # 替换为实际路径
find_package(PostgreSQL REQUIRED)

3. 检查环境变量设置

确保PostgreSQL的库和头文件路径在系统搜索路径中:

export PKG_CONFIG_PATH=/usr/local/pgsql/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH

设置完成后重新运行CMake构建。

4. 清理旧构建目录重新编译

旧构建缓存可能导致配置未更新,执行以下命令后重新构建:

rm -rf build
cmake -S . -B build
cd build && make

5. 检查Drogon版本兼容性

老版本Drogon可能与新版PostgreSQL存在兼容问题,建议升级到最新稳定版Drogon。

6. 编写测试程序验证支持状态

用简单程序确认Drogon是否真的启用了PostgreSQL支持:

#include <drogon/drogon.h>
#include <iostream>

int main() {
    if (drogon::app().isDbClientSupported("postgresql")) {
        std::cout << "PostgreSQL 支持正常" << std::endl;
    } else {
        std::cout << "PostgreSQL 不被支持" << std::endl;
    }
    return 0;
}

编译运行后,根据输出判断问题出在Drogon本身还是配置环节。

内容的提问来源于stack exchange,提问作者yagil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 15:27:34