psql コマンドだけちょこっと使いたい。 ただそれだけなのだが、クライアントのみのバイナリは配布されていない。
そんな時は、zip archive 版から不要ファイルを削除すれば良い。
PostgreSQL ダウンロードサイトから zip archive
のリンクを辿れば postgresql-16.4-1-windows-x64-binaries.zip
のような zip archive
が入手できる。
postgresql-16.4-1-windows-x64-binaries.zip
を解凍すると以下のようなフォルダ構成になっている。
このまま bin\psql.exe
を実行すれば psql コマンドは使えるが、サーバなど全部入りなのでディスク容量は 900M を超える。
bin
フォルダ以外は不要で、もっと言えば以下のファイルだけあれば psql コマンドは一応使えるし、ディスク容量も 8M 程度。
pg_dump
と pg_restore
も必要な場合は、pg_dump.exe
と pg_restore.exe
に加え、zlib1.dll
, liblz4.dll
, libzstd.dll
も残して置くと良い。
ダウンロードからファイル抽出までを行うPowerShellスクリプトは以下のようにできる。
$path = Split-Path -Parent $MyInvocation.MyCommand.Path Set-Location $path $filename = "postgresql-16.4-2-windows-x64-binaries.zip" if (-Not (Test-Path -Path $filename)) { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri https://get.enterprisedb.com/postgresql/$filename -OutFile $filename } $shell_app = new-object -com shell.application $zip_file = $shell_app.namespace((Get-Location).Path + "\$filename") $destination = $shell_app.namespace((Get-Location).Path) $zip_file.Items() | where Name -eq 'pgsql' | ? { $_.GetFolder.Items() | where Name -eq 'bin' | ? { foreach ($item in $_.GetFolder.Items()) { if ($item.Name -in @('libcrypto-3-x64.dll', 'libiconv-2.dll', 'libintl-9.dll', 'liblz4.dll', 'libpq.dll', 'libssl-3-x64.dll', 'libwinpthread-1.dll', 'libzstd.dll', 'pg_dump.exe', 'pg_restore.exe', 'psql.exe', 'zlib1.dll')) { $destination.Copyhere($item) } } } }
initpgsql.ps1
などとして保存して実行すれば、必要ファイルを収集できる。
bin
フォルダの横に以下のようなバッチファイル用意しておくと簡単。
@echo off setlocal chcp 65001 set PGCLIENTENCODING=utf-8 set PGHOST=localhost set PGPORT=5432 set PGDATABASE=postgres set PGUSER=postgres set PGPASSWORD=mypassword echo ---------------------------------------------- echo HOST: %PGHOST% %PGPORT% echo DATABASE: %PGDATABASE% echo USERNAME: %PGUSER% echo ---------------------------------------------- %~dp0bin\psql cmd /K endlocal :END