3

I'm a vagrant newbie trying to configure his first instance. But wherever I look on Vagrantfiles configuring PostgreSQL they're always using port forwarding to the host.

config.vm.network :forwarded_port, host: 5432, guest: 5432 

What is the point of forwarding database's port to the host?
Shouldn't the database be operating inside the box?
Or is it just a practice to have better data persistance?
And even if this is desired workflow and everything's forwarded then what is the point of installing the database inside the box after all?

Krzysztof Wende
  • 3,208
  • 25
  • 38

2 Answers2

2

This is mainly so that you can use Graphical Database SQL tools to query your Postgres database from your host machine. Since vagrant is used mainly by programmers and they need to frequently check the database for testing/debugging, this is in place. IDEs like IntelliJ and Eclipse have tools that you use to query databases and they would run on the host machine and thus would need to have access to the database port.

If you don't need to query your database from your host machine and it is only accessed by the guest VM, then you don't need this set.

ARau
  • 489
  • 2
  • 9
1

Shouldn't the database be operating inside the box?

Yes.

Or is it just a practice to have better data persistence?

There is no relation between port forwarding and data persistence.

And even if this is desired workflow and everything's forwarded then what is the point of installing the database inside the box after all?

What is the point of forwarding database's port to the host?

There is no need to always forward port. Port forwarding is only needed when you want to access that port from host machine, e.g. using graphical tools as pointed by blownie55's answer. If you don't need to access the port from host then there is no need to forward it.

And this is true for all kind of port forwarding, not only for database port.

As a side note, there are other ways to access resource inside guest from host, e.g. configuring a private network.

Community
  • 1
  • 1
taskinoor
  • 45,586
  • 12
  • 116
  • 142