Shark
Shark

Reputation: 43

How to connect postgres databases using ssh tunnel in ruby

Here is my problem, I want to access our database through ruby script, and I can connect to database through PSequal using ssh tunnel. When I tried to connect to database from ruby, it always return me time out error.

The Error MESSAGE IS LIKE THIS :"could not connect to server: Operation timed out Is the server running on host "HOSTNAME" (IP ADDRESS) and accepting TCP/IP connections on port 5432"

I have tried to use "psql -h -d -U" in terminal to login, but I got the same answer. Thanks

def connectDb
  begin
    file = File.open("pem file")
    gateway = Net::SSH::Gateway.new('hostname', 'username', keys_only: true, port: 22, key_data: file)
    port_pg = gateway.open('hostname', 5432)
    puts port_pg
    con = PG::Connection.open('hostname', portpg, "", "", 'dbname', 'username', 'password')
  rescue PG::Error => e
    puts e.message
  end
end

Upvotes: 3

Views: 2556

Answers (1)

jamesc
jamesc

Reputation: 12837

Make sure your Postgres server is listening on the correct port In postgresql.conf (Probably located somewhere like /etc/postgresql/9.3/main/postgresql.conf comment out

#listen_addresses = 'localhost'

and add a listen on all ports

listen_addresses = '*'

The Postgres config files will likely be owned by Postgres which can make them more difficult to find.

Log into your postgres database and run

SHOW config_file;

This should give you the location for you to then be able to edit the file

Have a look at this StackOverflow thread for further info on this After editing the file ( you will probably need sudo privileges to do this i.e. sudo vim path_to/config_file ) you will need to restart postgres for the changes to take effect. How you restart will depend on how pg is set up but most likely it is runing as a service so tyhe following should do the trick.

sudo service postgres restart

If not then ask your host how to restart

Create an entry in your database.yml file for the remote server e.g.

remote:
  adapter: postgresql
  database: remote_db_name
  username: xxx
  password: xxx
  pool: 5
  timeout: 5000
  host: ip_address_for_remote_server
  port: prob_3306_but_whatever_port_you_have_configured_on_remote_server
  strict: false

Then create a specific set of models to deal with the remote database and establish a connection with the database.yml entry

e.g.

class SomeTableNameOnRemoteServer < ActiveRecord::Base
    establish_connection :remote
    #etc...

Don't be tempted to try to use models that already connect to your local database. If you want to share logic then create a module and just include the module in both model classes

Some further reading might help. This is a guide on how to setup and configure pg on a DigitalOcean droplet

https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-postgresql-server-part-3

some way down the page there is a section on remote access but you may find the whole document useful so not pasting content in here.

Upvotes: 1

Related Questions