doubleB
doubleB

Reputation: 348

Vagrant postgres ssh tunnel

I have got the vagrant machine with Postgres on it. I need to connect with this database using some external tools (eg. pgmodeler, keetle). So i start tunnel using:

ssh -L 5433:127.0.0.1:5432 [email protected] -i puphpet/files/dot/ssh/id_rsa

Then I try to login with command:

psql -h 127.0.0.1 -p 5433 -U postgres postgres

And I get an error:

FATAL: password authentication failed for user "postgres"

I'm a little bit confused because it used to work, and now it doesn't. I've tried to set user password but it didn't work. Where should i look for a problem?

Upvotes: 1

Views: 1359

Answers (1)

dahrens
dahrens

Reputation: 3959

You need to adjust the pg_hba.conf file of your postgres server to tell postgres from where connections for which users are allowed. In debian-like distributions you'll find this file in /etc/postgresql/9.1/main/pg_hba.conf.

When developing with vagrant i usually use the following entry in pg_hba.conf which allows all users to connect from everywhere without a password

# IPv4 connections from everywhere:
host    all             all             0.0.0.0/0               trust

NEVER USE THIS LINE ON A PRODUCTION SERVER

On top there is the following line in my Vagrantfile to forward the port

Vagrant.configure(2) do |config|
    # more stuff
    config.vm.network "forwarded_port", guest: 5432, host: 5433, host_ip: "127.0.0.1"
end

Do not forget to set the host_ip to localhost, otherwise postgres binds to all network interfaces of your local machine.

Upvotes: 1

Related Questions