### Install Dependencies with Bundler Source: https://github.com/ged/ruby-pg/blob/master/README.md Run this command after checking out the source code to install all necessary project dependencies. ```bash $ bundle install ``` -------------------------------- ### Install PG Gem with Specific Config Source: https://github.com/ged/ruby-pg/blob/master/README.md Install the PG gem, forcing the use of the source gem and specifying the path to pg_config. Useful when binary gems are not suitable or available. ```bash gem install pg -- --with-pg-config= ``` -------------------------------- ### Install pg Gem with Specific pg_config Path Source: https://github.com/ged/ruby-pg/blob/master/README-OS_X.rdoc When installing the 'pg' gem, specify the path to the 'pg_config' tool using the --with-pg-config option if it's not in your default PATH or if you need to use a specific PostgreSQL installation. ```bash gem install -- --with-pg-config=/opt/local/lib/postgresql90/bin/pg_config ``` -------------------------------- ### Connect and Query PostgreSQL Source: https://github.com/ged/ruby-pg/blob/master/README.md Connect to a PostgreSQL database and execute a query to retrieve connection activity. Requires the 'pg' gem to be installed. ```ruby #!/usr/bin/env ruby require 'pg' # Output a table of current connections to the DB conn = PG.connect( dbname: 'sales' ) conn.exec( "SELECT * FROM pg_stat_activity" ) do |result| puts " PID | User | Query" result.each do |row| puts " %7d | %-16s | %s " % row.values_at('pid', 'usename', 'query') end end ``` -------------------------------- ### Install Specific Legacy Version of 'postgres' Gem Source: https://github.com/ged/ruby-pg/blob/master/misc/postgres/README.txt Use this command to install a specific legacy version of the 'postgres' gem if required for older projects. Ensure you uninstall any newer versions afterward. ```bash gem install postgres -v '0.7.9.2008.01.28' ``` ```bash gem uninstall postgres -v '>0.7.9.2008.01.28' ``` -------------------------------- ### Enable Basic Type Casting for Results and Queries Source: https://github.com/ged/ruby-pg/blob/master/README.md Configure type mapping for both retrieving results and sending query parameters. The first example shows how result values are cast, and the second demonstrates parameter casting. ```ruby conn.type_map_for_results = PG::BasicTypeMapForResults.new conn # ... this works for result value mapping: conn.exec("select 1, now(), '{2,3}'::int[]").values # => [[1, 2014-09-21 20:51:56 +0200, [2, 3]]] ``` ```ruby conn.type_map_for_queries = PG::BasicTypeMapForQueries.new conn # ... and this for param value mapping: conn.exec_params("SELECT $1::text, $2::text, $3::text", [1, 1.23, [2,3]]).values # => [["1", "1.2300000000000000E+00", "{2,3}"]] ``` -------------------------------- ### Install Specific Version of ruby-pg Source: https://github.com/ged/ruby-pg/blob/master/misc/ruby-pg/README.txt Use this command to install a specific, older version of the ruby-pg gem if required for legacy applications. Ensure you are aware of the version number needed. ```bash gem install ruby-pg -v '0.7.9.2008.01.28' ``` -------------------------------- ### Build 'pg' Gem with Custom PostgreSQL Path Source: https://github.com/ged/ruby-pg/blob/master/README-Windows.rdoc Use this command when 'pg_config.exe' is not in your PATH. Adjust the path to your PostgreSQL installation, ensuring you use short path names to avoid issues with spaces. ```bash rake --with-pg-dir=c:/progra~1/postgr~1/8.3 ``` -------------------------------- ### Install pg Gem with ARCHFLAGS for Universal Binaries Source: https://github.com/ged/ruby-pg/blob/master/README-OS_X.rdoc If you encounter issues with universal binaries during 'pg' gem installation on MacOS X, set the ARCHFLAGS environment variable to specify the desired architecture, such as x86_64. ```bash sudo env ARCHFLAGS='-arch x86_64' gem install pg ``` -------------------------------- ### Check OpenSSL Library Linking on MacOS X Source: https://github.com/ged/ruby-pg/blob/master/README-OS_X.rdoc Use 'otool -L' to verify which SSL library your Ruby installation is linked against. This is crucial for preventing segfaults when using 'pg' and the 'openssl' extension simultaneously. ```bash $ otool -L /System/Library/Frameworks/Ruby.framework/Versions\ /1.8/usr/lib/ruby/1.8/universal-darwin11.0/openssl.bundle /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/\ lib/ruby/1.8/universal-darwin11.0/openssl.bundle: /System/Library/Frameworks/Ruby.framework/Versions/1.8/ usr/lib/libruby.1.dylib (compatibility version 1.8.0, \ current version 1.8.7) /usr/lib/libssl.0.9.8.dylib (compatibility version 0.9.8, \ current version 0.9.8) /usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, \ current version 0.9.8) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, \ current version 159.0.0) ``` -------------------------------- ### Uninstall Newer Versions of ruby-pg Source: https://github.com/ged/ruby-pg/blob/master/misc/ruby-pg/README.txt If you have a newer version of ruby-pg installed and need to use an older one, use this command to uninstall any versions greater than the specified one. This is crucial for ensuring compatibility with legacy systems. ```bash gem uninstall ruby-pg -v '>0.7.9.2008.01.28' ``` -------------------------------- ### Build Native Binary 'pg' Gems for Windows Source: https://github.com/ged/ruby-pg/blob/master/README-Windows.rdoc This command initiates the process of building native binary gems for Windows using Docker. It downloads a Docker image, builds OpenSSL and PostgreSQL within the container, and then compiles the gem for supported Ruby versions. ```bash rake gem:native ``` -------------------------------- ### Run Tests Source: https://github.com/ged/ruby-pg/blob/master/README.md Execute the project's tests and specs against the PostgreSQL version configured by `pg_config --bindir`. ```bash $ rake test ``` -------------------------------- ### Compile Extension Source: https://github.com/ged/ruby-pg/blob/master/README.md Compile the extension files for the project. ```bash $ rake compile ``` -------------------------------- ### Generate API Documentation Source: https://github.com/ged/ruby-pg/blob/master/README.md Generate the API documentation for the project using the rake task. ```bash $ rake docs ``` -------------------------------- ### Run Specific Test with PostgreSQL Version Source: https://github.com/ged/ruby-pg/blob/master/README.md Run a specific test file and line number against a particular PostgreSQL version by prepending the desired PostgreSQL bin directory to the PATH. ```bash $ PATH=/usr/lib/postgresql/14/bin:$PATH rspec -Ilib -fd spec/pg/connection_spec.rb:455 ``` -------------------------------- ### Configure Bundler for Platforms Source: https://github.com/ged/ruby-pg/blob/master/README.md Lock down specific platforms for the bundle and package all platform-specific gems. Ensures consistent builds across different environments. ```bash bundle lock --add-platform x86_64-linux bundle lock --add-platform arm64-darwin bundle lock --add-platform x64-mingw-ucrt bundle lock --add-platform ruby bundle package --all-platforms ``` -------------------------------- ### Clean Extension Files Source: https://github.com/ged/ruby-pg/blob/master/README.md Use this rake task to clean up extension files, packaging files, and test databases. It's useful for switching between different PostgreSQL versions. ```bash $ rake clean ``` -------------------------------- ### Compile pg Gem with ARCHFLAGS Source: https://github.com/ged/ruby-pg/blob/master/README-OS_X.rdoc When building the 'pg' gem from source on MacOS X, you can explicitly set the ARCHFLAGS environment variable to ensure compatibility with specific architectures. ```bash rake compile ARCHFLAGS="-arch x86_64" ``` -------------------------------- ### Force Source Gem Usage in Gemfile Source: https://github.com/ged/ruby-pg/blob/master/README.md Force the usage of the source gem for the 'pg' gem within a Gemfile. Use this if binary gems are causing issues or do not support a required feature. ```ruby gem "pg", force_ruby_platform: true ``` -------------------------------- ### Add PG Dependency with Pessimistic Version Constraint Source: https://github.com/ged/ruby-pg/blob/master/README.md Specify a dependency on the 'pg' gem in a gemspec file using a pessimistic version constraint. This follows Semantic Versioning principles for reliable updates. ```ruby spec.add_dependency 'pg', '~> 1.0' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.