Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant: Why forward database ports?

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?

like image 456
Krzysztof Wende Avatar asked Nov 24 '25 13:11

Krzysztof Wende


2 Answers

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.

like image 184
ARau Avatar answered Nov 28 '25 16:11

ARau


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.

like image 25
taskinoor Avatar answered Nov 28 '25 15:11

taskinoor