Setup a Static IP for Ruby PostgreSQL

Learn how to route Ruby PostgreSQL database traffic through a QuotaGuard Static IP proxy.

Prerequisites

# install QGTunnel
curl https://s3.amazonaws.com/quotaguard/qgtunnel-latest.tar.gz | tar xz

# Setup Tunnel in QuotaGuard Dashboard
https://dash.quotaguard.com/setup/tunnels (or provide `.qgtunnel` file)

Instructions

Testing in Docker
docker build -t qg-static-ruby-postgres-example .
docker run -e QUOTAGUARDSTATIC_URL=<your_quotaguardstatic_url> \
           -e RDS_DB_NAME=<your_database_name> \
           -e RDS_PASSWORD=<your_database_password> \
           qg-static-ruby-postgres-example

Code Samples

app.rb
require 'pg'

begin
  connection = PG.connect(
    dbname: ENV['RDS_DB_NAME'],
    user: 'postgres',
    password: ENV['RDS_PASSWORD'],
    host: '127.0.0.1',
    port: 5432,
    connect_timeout: 60
  )

  puts "Successfully connected to the database"
  
  # Run a simple query to verify
  result = connection.exec("SELECT 1")
  result.each do |row|
    puts row
  end

rescue PG::Error => e
  puts "Error connecting to the database: #{e.message}"
ensure
  connection.close if connection
end

Docker File
# Start from a Ruby base image
FROM ruby:latest

# Update the package list
RUN apt-get update -qq && apt-get install -y curl

# Set the working directory
WORKDIR /usr/src/app

# Download and extract QGTunnel software
RUN curl https://s3.amazonaws.com/quotaguard/qgtunnel-latest.tar.gz | tar xz

# Install pg gem
RUN gem install pg

# Copy your Ruby script into the Docker image
COPY app.rb .

# Copy the QGTunnel configuration file into the Docker image (uncomment if you have a configuration file)
# COPY .qgtunnel .


# Set the command to run the QGTunnel process and your Ruby script
CMD ["bin/qgtunnel", "ruby", "app.rb"]