ConservationInternational / COA001

COASST Database App & API
0 stars 0 forks source link

Can't connect to database from Knex #7

Closed seansu4you87 closed 9 years ago

seansu4you87 commented 9 years ago

Hey @d-g-h I'm having issues with the way the COA001 box is set up. From the pg-commander screenshot, it looks like you have to ssh into the machine first. I tried to tunnel into the machine but couldn't figure out the proper command to do so.

The interface I can specific with [Knex][0] is just host, port, database, user, and password. Can you help by relaxing the ssh requirement, or telling me how to properly tunnel into the COA001 box?

I'm currently using my own Vagrant postgres instance to do development on, so it's not a big issue for now.

d-g-h commented 9 years ago

@seansu4you87

I don't vagrant ssh. The VM should be thought as an external server. The port on the server forwards the standard postGRES port (5432) to your standard postGRES port.

To connect to the database remotely, we need to do this over SSH which port is 22, but is forwarded to the host machines port 2227, as to avoid conflict. This port doesn't autocorrect, so if you are running something locally on your host at 2227, you might have problems. This port is locked for the GUI tools, and I don't want to have reconfigure the settings.

screen shot 2015-02-09 at 9 04 03 am screen shot 2015-02-09 at 9 03 51 am

So,

host, port, database, user, and password.

host = localhost(127.0.0.1), port = 5432, database COA001 user = postgres, and password = postgres

seansu4you87 commented 9 years ago

Hey @d-g-h I'm still a bit confused. Knex connects like so:

var knex = require('knex')({
  client: 'pg',
  connection: {
    user: username,
    password: password,
    host: host,
    port: port,
    database: database,
  }
});

Filling in those arguments doesn't end up working:

~/Development/github.com/RadishLab/COA001/src/db master * knex migrate:currentVersion
Error: read ECONNRESET
    at errnoException (net.js:901:11)
    at TCP.onread (net.js:556:19)
~/Development/github.com/RadishLab/COA001/src/db master *

Knex does provide connection over ssh capabilities. Any suggestions for how to deal with this? I was originally trying to ssh tunnel into COA001, so that localhost:5432 would reference the postgres server inside COA001. However, I had some trouble with the command and wrote this issue. Should I keep trying there, or are we going to open up the box to remote connections?

seansu4you87 commented 9 years ago

Ok I got the tunnel to work:

ssh -L 5432:localhost:5432 vagrant@localhost -p 2227 -i .vagrant/machines/default/virtualbox/pri
vate_key

I'm creating a tunnel into the vagrant box, forwarding my port 5432 into localhost:5432 on the box. When I do this, the Knex connection works out!

d-g-h commented 9 years ago

Vagrant (VirtualBox) forwards to ports for you (should?), hence,

https://github.com/RadishLab/COA001/blob/51ed709e59170ccadafde3dff83e25cd98402313/devops/Vagrantfile#L15

and

https://github.com/RadishLab/COA001/blob/51ed709e59170ccadafde3dff83e25cd98402313/devops/Vagrantfile#L16

I'm glad that worked out though.

seansu4you87 commented 9 years ago

@d-g-h something is still funky. Although the Vagrantfile configured port forwarding for 5432 on my machine to 5432 on the box, trying to connect to localhost:5432 would error, unless I ssh tunneled into localhost:2227 (this 2227 => 22 forwarding works).

d-g-h commented 9 years ago

@seansu4you87 isn't it 192.168.50.210:5432 on the host machine.

This works because

trying to connect to localhost:5432 would error, unless I ssh tunneled into localhost:2227 (this 2227 => 22 forwarding works).

you are hitting the guest machines localhost file.

d-g-h commented 9 years ago

Though, I must say, I only connect over ssh. As you would do this over the public network, or at Starbucks.