PG gem does not work on windows with ruby 2.5 and greater

Issue #275 resolved
cshupp
created an issue

Consider the following:

(shell setup)

set PATH=C:\languages\ruby\Ruby23-x64\bin;C:\databases\pg96\bin
set GEM_HOME=c:\temp\r23

gem install pg -- --with-pg-config=C:\databases\pg96\bin
Fetching: pg-1.0.0-x64-mingw32.gem (100%)
Successfully installed pg-1.0.0-x64-mingw32
Parsing documentation for pg-1.0.0-x64-mingw32
Installing ri documentation for pg-1.0.0-x64-mingw32
Done installing documentation for pg after 5 seconds
1 gem installed

Now in irb:

irb(main):001:0> RUBY_VERSION
=> "2.3.3"
irb(main):002:0> RUBY_PLATFORM
=> "x64-mingw32"
irb(main):003:0> require 'pg'
=> true
irb(main):040:0> PG.connect( dbname: 'drturbotax_development', user: 'deap', password: 'something' ).exec( "select count(*) as datname from referral_approvals;" ).to_a.first
=> {"datname"=>"2000"}

Ruby 2.3 works!

Now for 2.4:

(shell setup)

set PATH=C:\languages\ruby\Ruby24-x64\bin;C:\databases\pg96\bin
set GEM_HOME=c:\temp\r24

gem install pg -- --with-pg-config=C:\databases\pg96\bin
Fetching: pg-1.0.0-x64-mingw32.gem (100%)
Successfully installed pg-1.0.0-x64-mingw32
Parsing documentation for pg-1.0.0-x64-mingw32
Installing ri documentation for pg-1.0.0-x64-mingw32
Done installing documentation for pg after 4 seconds
1 gem installed

irb:

C:\work\cui_work\cui>irb
irb(main):001:0> RUBY_VERSION
=> "2.4.1"
irb(main):002:0> RUBY_PLATFORM
=> "x64-mingw32"
irb(main):003:0> require 'pg'
=> true
irb(main):004:0> PG.connect( dbname: 'drturbotax_development', user: 'deap', password: 'something' ).exec( "select count(*) as datname from referral_approvals;" ).to_a.first
=> {"datname"=>"2000"}
irb(main):005:0>

2.4 works!

Now for 2.5

(shell setup)

set PATH=C:\languages\ruby\Ruby25-x64\bin;C:\databases\pg96\bin
set GEM_HOME=c:\temp\r25

gem install pg -- --with-pg-config=C:\databases\pg96\bin
Fetching: pg-1.0.0-x64-mingw32.gem (100%)
Successfully installed pg-1.0.0-x64-mingw32
Parsing documentation for pg-1.0.0-x64-mingw32
Installing ri documentation for pg-1.0.0-x64-mingw32
Done installing documentation for pg after 3 seconds
1 gem installed

irb:

irb(main):001:0> RUBY_VERSION
=> "2.5.0"
irb(main):002:0> RUBY_PLATFORM
=> "x64-mingw32"
irb(main):003:0> require 'pg'
Traceback (most recent call last):
       12: from C:/languages/ruby/Ruby25-x64/bin/irb.cmd:19:in `<main>'
       11: from (irb):3
       10: from C:/languages/ruby/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:39:in `require'
        9: from C:/languages/ruby/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
        8: from C:/languages/ruby/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `require'
        7: from c:/temp/r25/gems/pg-1.0.0-x64-mingw32/lib/pg.rb:3:in `<top (required)>'
        6: from c:/temp/r25/gems/pg-1.0.0-x64-mingw32/lib/pg.rb:24:in `rescue in <top (required)>'
        5: from c:/temp/r25/gems/pg-1.0.0-x64-mingw32/lib/pg.rb:12:in `block in <top (required)>'
        4: from c:/temp/r25/gems/pg-1.0.0-x64-mingw32/lib/pg.rb:18:in `rescue in block in <top (required)>'
        3: from c:/temp/r25/gems/pg-1.0.0-x64-mingw32/lib/pg.rb:25:in `block in <top (required)>'
        2: from C:/languages/ruby/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
        1: from C:/languages/ruby/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
LoadError (127: The specified procedure could not be found.   - c:/temp/r25/gems/pg-1.0.0-x64-mingw32/lib/2.5/pg_ext.so)
irb(main):004:0>

A few diff (there are none):

c:\temp>diff C:\temp\r24\gems\pg-1.0.0-x64-mingw32\lib\libpq.dll C:\temp\r24\gems\pg-1.0.0-x64-mingw32\lib\libpq.dll

c:\temp>

c:\temp>diff C:\temp\r24\gems\pg-1.0.0-x64-mingw32\lib\2.5\pg_ext.so C:\temp\r25\gems\pg-1.0.0-x64-mingw32\lib\2.5\pg_ext.so

c:\temp>

The results are the same for ruby 2.5.1 and switching down to gem version:

pg (0.21.0-x64-mingw32)

also yields the same result.

Lastly, if it helps:

C:\work\cui_work\cui>cd c:\databases\pg96\bin

c:\databases\pg96\bin>pg_config
BINDIR = c:/DATABA~1/pg96/bin
DOCDIR = c:/DATABA~1/pg96/share/doc/POSTGR~1
HTMLDIR = c:/DATABA~1/pg96/share/doc/POSTGR~1
INCLUDEDIR = c:/DATABA~1/pg96/include
PKGINCLUDEDIR = c:/DATABA~1/pg96/include/POSTGR~1
INCLUDEDIR-SERVER = c:/DATABA~1/pg96/include/POSTGR~1/server
LIBDIR = c:/DATABA~1/pg96/lib
PKGLIBDIR = c:/DATABA~1/pg96/lib/POSTGR~1
LOCALEDIR = c:/DATABA~1/pg96/share/locale
MANDIR = c:/databases/pg96/share/man
SHAREDIR = c:/DATABA~1/pg96/share/POSTGR~1
SYSCONFDIR = c:/databases/pg96/etc/postgresql
PGXS = c:/DATABA~1/pg96/lib/POSTGR~1/pgxs/src/MAKEFI~1/pgxs.mk
CONFIGURE = '--prefix=C:/msys64/opt/pgbin-build/pgbin/bin/../20170831_0525/build/pg96-9.6.5-1-win64' '--enable-integer-datetimes' '--enable-thread-safety' '--with-libxml' '--with-libxslt' '--with-ossp-uuid' '--enable-
nls' '--with-openssl' '--with-ldap' '--with-python' '--with-perl' '--with-tcl' '--with-libraries=/opt/pgbin-build/pgbin/shared/win64/lib:/usr/local/lib:/usr/local/ssl/lib' '--with-includes=/usr/local/include:/usr/loca
l/ssl/include' 'CFLAGS=-O2 -DMS_WIN64 -I/opt/pgbin-build/pgbin/shared/win64/include'
CC = gcc
CPPFLAGS = -DFRONTEND -I./src/include/port/win32 -DEXEC_BACKEND -I/usr/local/include/libxml2 -I/usr/local/include -I/usr/local/ssl/include -I../../src/include/port/win32 -DBUILDING_DLL
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -DMS_WIN64 -I/
opt/pgbin-build/pgbin/shared/win64/include
CFLAGS_SL =
LDFLAGS = -L../../src/common -Wl,--allow-multiple-definition -Wl,--disable-auto-import -L/usr/local/lib -LC:/msys64/mingw64/lib -L/opt/pgbin-build/pgbin/shared/win64/lib -L/usr/local/lib -L/usr/local/ssl/lib -Wl,--as-
needed
LDFLAGS_EX =
LDFLAGS_SL =
LIBS = -lpgcommon -lpgport -lintl -lxslt -lxml2 -lssl -lcrypto -lz -lws2_32 -lm  -lws2_32
VERSION = PostgreSQL 9.6.5

Comments (6)

  1. Lars Kanis

    Thank you for this very detailed report!

    I'll didn't notice any issues with RubyInstaller-2.5 so far, but I'll investigate it. This can take a couple of days, since I'm pretty busy currently.

    One thing I noticed is that you're using the tar binary gem on all ruby versions(no--platform option), so that the option --with-pg-config= is ignored, but the built-in libpq.dll is used instead.

  2. cshupp reporter

    Thanks, I added the platform tag, and get a different error:

    C:\WINDOWS\system32> gem install pg --platform x64_mingw -- --with-pg-config=C:\databases\postgres\pg96\bin\pg_config
    Fetching: pg-1.0.0.gem (100%)
    Temporarily enhancing PATH for MSYS/MINGW...
    Building native extensions with: '--with-pg-config=C:\databases\postgres\pg96\bin\pg_config'
    This could take a while...
    Successfully installed pg-1.0.0
    Parsing documentation for pg-1.0.0
    Installing ri documentation for pg-1.0.0
    Done installing documentation for pg after 2 seconds
    1 gem installed
    
    C:\WINDOWS\system32>irb
    irb(main):001:0> RUBY_VERSION
    => "2.5.0"
    irb(main):002:0> require 'pg'
    Traceback (most recent call last):
           12: from C:/languages/ruby/Ruby25-x64/bin/irb.cmd:19:in `<main>'
           11: from (irb):2
           10: from C:/languages/ruby/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:39:in `require'
            9: from C:/languages/ruby/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
            8: from C:/languages/ruby/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `require'
            7: from c:/temp/r25/gems/pg-1.0.0/lib/pg.rb:3:in `<top (required)>'
            6: from c:/temp/r25/gems/pg-1.0.0/lib/pg.rb:24:in `rescue in <top (required)>'
            5: from c:/temp/r25/gems/pg-1.0.0/lib/pg.rb:12:in `block in <top (required)>'
            4: from c:/temp/r25/gems/pg-1.0.0/lib/pg.rb:18:in `rescue in block in <top (required)>'
            3: from c:/temp/r25/gems/pg-1.0.0/lib/pg.rb:25:in `block in <top (required)>'
            2: from C:/languages/ruby/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
            1: from C:/languages/ruby/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    LoadError (cannot load such file -- 2.5/pg_ext)
    irb(main):003:0>
    
  3. Lars Kanis

    You should use --platform ruby to switch to the source gem. However the source gem requires to set RUBY_DLL_PATH=C:\databases\postgres\pg96\bin\ , so that it finds the lipq.dll and others at runtime. I think this should be set automatically on Windows, but I didn't have the time to implement it, so far.

  4. Lars Kanis

    Alternatively you can run ridk exec pacman -S mingw-w64-x86_64-postgresql to install the PostgreSQL package of MSYS2/MINGW. Then gem inst pg -- --platform ruby without additional paths. This will find all dlls without RUBY_DLL_PATH.

  5. cshupp reporter

    You are a miracle worker! Thanks:

    C:\WINDOWS\system32> gem uninstall pg
    Successfully uninstalled pg-1.0.0
    
    C:\WINDOWS\system32>set RUBY_DLL_PATH=C:\databases\postgres\pg96\bin\
    
    C:\WINDOWS\system32> gem install pg --platform ruby -- --with-pg-config=C:\databases\postgres\pg96\bin\pg_config
    Fetching: pg-1.0.0.gem (100%)
    Temporarily enhancing PATH for MSYS/MINGW...
    Building native extensions with: '--with-pg-config=C:\databases\postgres\pg96\bin\pg_config'
    This could take a while...
    Successfully installed pg-1.0.0
    Parsing documentation for pg-1.0.0
    Installing ri documentation for pg-1.0.0
    Done installing documentation for pg after 4 seconds
    1 gem installed
    
    C:\WINDOWS\system32>irb
    irb(main):001:0> require 'pg'
    => true
    <word: 'something' ).exec( "select count(*) as datname from referral_approvals;" ).to_a.first
    => {"datname"=>"2000"}
    irb(main):003:0>
    
  6. Log in to comment