LoginSignup
1
0

More than 1 year has passed since last update.

Windows10 で Ruby3 + pg gem を利用して postgreSQL にアクセスするとエラー `AddDllDirectory failed for x64-mingw32` になる

Posted at

現象

bundle exec rake db:migrate したけどエラーが発生する

PS C:\app> bundle exec rake db:migrate
rake aborted!
RubyInstaller::Runtime::DllDirectory::WinApiError: AddDllDirectory failed for C:/app/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3/lib/x64-mingw32
C:/app/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3/lib/pg.rb:15:in `block in <top (required)>'
C:/app/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3/lib/pg.rb:26:in `rescue in <top (required)>'
(略)

LoadError: 126: 指定されたモジュールが見つかりません。   - C:/app/vendor/bundle/ruby/3.0.0/gems/pg-1.2.3/lib/pg_ext.so
C:/app/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/dependencies.rb:332:in `require'
C:/app/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4.1/lib/active_support/dependencies.rb:332:in `block in require'
(略)
(See full trace by running task with --trace)

結論

環境変数 RUBY_DLL_PATH<your-path-to-postgresql>/bin をセットして実行する。

Powershell の場合(VSCodeの規定値?)

$env:RUBY_DLL_PATH='C:/Program Files/PostgreSQL/13/bin/'

コマンドプロンプトの場合

set RUBY_DLL_PATH=<your-path-to-postgresql>/bin

調べたこと

RubyInstaller::Runtime::DllDirectory::WinApiError: AddDllDirectory failed for x64-mingw32 でぐぐると、
pg の公式GitHubのIssue(Ruby 3 Windows 10 Gem load Error · Issue #365 · ged/ruby-pg)がでてきました。

そこでコラボレーターさんが以下のようにコメントしていました(意訳)。

  • Ruby3.0用のにアップデートしたいと思ってるよ
  • それまでは以下の3つの回避方法があるよ
    1. install postgresql per pacman: ridk exec pacman -S mingw-w64-x86_64-postgresql and reinstall pg gem(よくわからなかったので原文)
    2. 実行前に set RUBY_DLL_PATH=<your-path-to-postgresql>/bin してね
    3. Ruby を 3.0 未満にしてね

自分は、あまり考えず 2. でやってみたところ、無事動いたのでこれで良しとしました。
なお、VSCodeのターミナルが PowerShell だったことを忘れていて30分ぐらい格闘してました泣

1
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
1
0