0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows環境のGCCを用いて、PostgreSQL libpqからSQLを実行する

0
Last updated at Posted at 2026-11-13

概要

Windows環境でC言語からPostgreSQLを操作する。

事前準備

PostgreSQLインストール

  • 現時点の最新バージョン(18.7-1)をインストール

MSYS2インストール

MSYS2内に必要なGCCとPostgreSQLをインストールするコマンド

C:\msys64\ucrt64.exeでターミナルを起動し、

pacman -S mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-postgresql

を実行し、GCCとPostgreSQLをインストールする。

image.png

フォルダ構成

.
├── start.bat
└── test.c

実行するCソースコード

test.c
#include <libpq-fe.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
  const char* conninfo = "host=localhost port=5432 dbname=postgres user=postgres password=postgres";

  PGconn* conn = PQconnectdb(conninfo);

  if (PQstatus(conn) != CONNECTION_OK) {
    fprintf(stderr, "接続失敗: %s\n", PQerrorMessage(conn));
    PQfinish(conn);
    exit(1);
  }

  printf("接続成功!! 接続先のPostgreSQLバージョン: %s\n", PQparameterStatus(conn, "server_version"));

  // PQprintOpt の設定
  PQprintOpt options = {0};  // すべてゼロで初期化
  options.header   = 1;      // ヘッダー(列名)を表示
  options.align    = 1;      // 揃えて表示
  options.fieldSep = "|";    // 区切り文字
  //options.expanded = 1;      // 縦展開形式

  PGresult* res;

  // SQL
  res = PQexec(conn, "SELECT version();");
  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
      // エラー
      fprintf(stderr, "クエリ失敗: %s\n", PQerrorMessage(conn));
      PQclear(res);
      PQfinish(conn);
      exit(1);
  } else {
      // 結果出力
      // printf("%s\n", PQgetvalue(res, 0, 0));
      PQprint(stdout, res, &options);
      PQclear(res);
  }

  PQfinish(conn);
  return 0;
}

ビルド&実行用のバッチ

start.bat
@echo off

@rem CソースファイルがUTF-8の想定のため、コマンドプロンプトのエンコーディング変更
chcp 65001

@rem 必要な環境変数を取得
for /f "usebackq" %%i in (`"C:\msys64\ucrt64\bin\pg_config.exe --includedir"`) do set includedir=%%i
echo %includedir%
for /f "usebackq" %%i in (`"C:\msys64\ucrt64\bin\pg_config.exe --libdir"`) do set libdir=%%i
echo %libdir%

@rem コンパイラバージョンを確認

echo MSYS2内のGCCバージョン
C:\msys64\ucrt64\bin\gcc.exe --version

@rem インクルードされるPostgreSQLバージョンを確認

echo MSYS2内のPostgreSQLバージョン
C:\msys64\ucrt64\bin\pg_config.exe --version

@rem gcc.exeが必要とするdllが配置されたフォルダを一時的に環境変数に追加
set PATH=C:\msys64\ucrt64\bin;%PATH%

@rem コンパイル実行
C:\msys64\ucrt64\bin\gcc.exe -o test .\test.c -I%includedir% -L%libdir% -lpq

@rem 完成した実行ファイルの動作確認
.\test.exe

pause

実行結果

Active code page: 65001
C:/msys64/ucrt64/include
C:/msys64/ucrt64/lib
MSYS2内のGCCバージョン
gcc.exe (Rev4, Built by MSYS2 project) 16.2.0
Copyright (C) 2026 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

MSYS2内のPostgreSQLバージョン
PostgreSQL 18.6
接続成功!! 接続先のPostgreSQLバージョン: 18.7
version
-----------------------------------------------------------------------
PostgreSQL 18.7 on x86_64-windows, compiled by msvc-19.44.35229, 64-bit
(1 row)

Press any key to continue . . .
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?