简体   繁体   English

如何在ruby中使用ssh隧道连接postgres数据库

[英]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. 这是我的问题,我想通过ruby脚本访问我们的数据库,我可以使用ssh隧道通过PSequal连接到数据库。 When I tried to connect to database from ruby, it always return me time out error. 当我尝试从ruby连接到数据库时,它总是让我超时错误。

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" 错误消息就像这样:“无法连接到服务器:操作超时服务器是否在主机上运行”HOSTNAME“(IP ADDRESS)并接受端口5432上的TCP / IP连接”

I have tried to use "psql -h -d -U" in terminal to login, but I got the same answer. 我试图在终端中使用“psql -h -d -U”登录,但我得到了相同的答案。 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

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 确保你的Postgres服务器正在侦听正确的端口在postgresql.conf中(可能位于/etc/postgresql/9.3/main/postgresql.conf注释掉的地方)

#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. Postgres配置文件可能归Postgres所有,这可能使它们更难找到。

Log into your postgres database and run 登录您的postgres数据库并运行

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 ie sudo vim path_to/config_file ) you will need to restart postgres for the changes to take effect. 看看这个 StackOverflow线程有关此内容的更多信息在编辑文件后(您可能需要sudo权限来执行此操作,即sudo vim path_to/config_file ),您需要重新启动postgres才能使更改生效。 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. 你如何重新启动将取决于pg的设置方式,但最有可能的是它作为一项服务运行,因此以下应该可以解决问题。

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 eg 在远程服务器的database.yml文件中创建一个条目,例如

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 然后创建一组特定的模型来处理远程数据库并与database.yml条目建立连接

eg 例如

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 这是有关如何在DigitalOcean Droplet上设置和配置pg的指南

https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-postgresql-server-part-3 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. 在页面的某个方面有一个关于远程访问的部分,但您可能会发现整个文档很有用,因此不会在此处粘贴内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM