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?

More than 3 years have passed since last update.

PostgreSQLをWindows上でソースコードからビルドしてplpythonを使う方法

Posted at

VisualStudioのCコンパイラを使ってPostgreSQLをソースコードからビルドする方法をまとめました。
PL/Pythonを使えるようにするには設定が必要なため、本記事で説明します。

今回使用したバージョンはPostgreSQL14.2ですが、他のバージョンでも同様にできると思います。

事前準備

今回使用したツールとバージョンは以下のとおりです。

  • Windows 10
  • Visual Studio 2019
  • Active Perl 5.28
  • Active Tcl 8.6.9
  • flex 2.6.4
  • biosn 2.7
  • Python 3.7.8

Active Perl/Tclおよびflex/bisonのインストールについては、関連記事1の"事前準備"の章をご覧ください。
PythonはVisual Studioに付属のものを使用しました。

Pythonの実行ファイルのパスの確認

こちらのサイトを参考に、Pythonの実行ファイル(python.exe)のパスを確認します。
まずコマンドプロンプトを起動し、pyコマンドを実行します。

>py
Python 3.7.8 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Pythonスクリプトを入力できるようになるので、下記2行を入力します。

import sys
sys.exec_prefix

以下のようにインストールフォルダが出力されるので覚えておきます。

>>> import sys
>>> sys.exec_prefix
'C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python37_64'

ソースコードのダウンロード

PostgreSQLのソースコードはPostgreSQL公式サイトからダウンロードしました。

圧縮ファイルを解凍し、C:\Projects\postgresql-14.2 に置くこととします。

Visual Studioビルド用設定ファイルの準備

PL/Pythonを使うには設定が必要ですので、本章で説明します。
デフォルト設定の場合、本章の操作は不要です。

src\tools\msvcフォルダに設定ファイルのサンプルconfig_default.plがあるので、そのファイルをconfig.plというファイル名でコピーします。

次にそのconfig.plをテキストエディタで編集します。
以下の行を変更します。

	python    => undef,    # --with-python=<path>

undefのところを、Pythonの実行ファイルのフォルダを設定します。

	python    => "C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python37_64",    # --with-python=<path>

ビルド

以降は関連記事1の操作と同じです。
Visual Studioのコマンドプロンプトを開き、src\tools\msvcフォルダでmkvcbuildを実行します。
bisonやflex関連のエラーが発生した場合は、関連記事1の"ソースコードのダウンロードと設定変更"の章をご覧ください。
mkvcbuild

Visual Studioのプロジェクトファイルが作成されるので、トップフォルダ(今回はC:\Projects\postgresql-14.2)にあるpgsql.slnというソリューションファイルを開きます。
PLs/plpython3というプロジェクトがあるはずです。

あとは、「ソリューションのビルド(B)」を実行すればOKです。
sln.png

トップフォルダ下に、デバッグビルドの場合はDebug、リリースビルドの場合はReleaseフォルダが作成されているはずです。

インストール

Visual Studioのコマンドプロンプト上で、src\tools\msvcフォルダでinstallを実行します。引数には、インストール先を指定します。
今回は、トップフォルダの下のpostgres1(C:\Projects\postgresql-14.2\postgres1)を指定しました。
install

サーバの起動とデータベースの作成

インストール先のbinフォルダには実行プログラムが入っているのでこれらを使用します。
例えば、インストールフォルダの下にdatabasesをデータベースクラスタとする場合、以下の手順でサーバを起動します。

  1. コマンドプロンプトを起動し、binフォルダに移動します。
  2. .\initdb -D ..\databases でデータベースクラスタを作成します。
  3. .\pg_ctl -D ..\databases start でPostgreSQLサーバを起動します。
  4. .\createdb test でtestというデータベースを作成します。

PL/Pythonの使用準備

まず .\psql test でpsqlにてtestデータベースに接続します。

PL/Python extensionが使用可能かを確認します。
pg_available_extensionsをSELECTして、plpython3uが存在すればOKです。

test=# SELECT * FROM pg_available_extensions WHERE name = 'plpython3u';
    name    | default_version | installed_version |                  comment
------------+-----------------+-------------------+-------------------------------------------
 plpython3u | 1.0             |                   | PL/Python3U untrusted procedural language
(1 row)

次にplpython3uを有効化し、Pythonの実行ファイルのパスを返すストアドファンクションをPL/Pythonで作成してみます。
SELECTして結果が返ればOKです。

test=# CREATE EXTENSION plpython3u;
CREATE EXTENSION
test=# CREATE FUNCTION func1 (OUT path text) AS $$
test$# import sys
test$# return sys.exec_prefix
test$# $$ LANGUAGE 'plpython3u';
CREATE FUNCTION
test=# SELECT func1();
                               func1
-------------------------------------------------------------------
 C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
(1 row)

おわりに

PL/Pythonを使うためのVisualStudioでのPostgreSQLビルド方法を説明しました。
今回はPL/Pythonを使いたかったのでconfig.plファイルの変更は1行のみでしたが、config.plを見ると、linuxでconfigureで指定するオプションの一部がWindowsでも指定できるようです。
ご参考までに設定項目を掲載しておきます。

	asserts => 0,    # --enable-cassert
	# blocksize => 8,         # --with-blocksize, 8kB by default
	# wal_blocksize => 8,     # --with-wal-blocksize, 8kB by default
	ldap      => 1,        # --with-ldap
	extraver  => undef,    # --with-extra-version=<string>
	gss       => undef,    # --with-gssapi=<path>
	icu       => undef,    # --with-icu=<path>
	lz4       => undef,    # --with-lz4=<path>
	nls       => undef,    # --enable-nls=<path>
	tap_tests => undef,    # --enable-tap-tests
	tcl       => undef,    # --with-tcl=<path>
	perl      => undef,    # --with-perl=<path>
	python    => undef,    # --with-python=<path>
	openssl   => undef,    # --with-ssl=openssl with <path>
	uuid      => undef,    # --with-uuid=<path>
	xml       => undef,    # --with-libxml=<path>
	xslt      => undef,    # --with-libxslt=<path>
	iconv     => undef,    # (not in configure, path to iconv)
	zlib      => undef     # --with-zlib=<path>

参考文献

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?