Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Postgres database inside Dokku container from outside

I'm having a hard time finding a way to access Postgres database inside Dokku's contaner (based on docker) from outside of the machine using pgAdmin. Is there a way to do that? Do I need to use a different client? I'm exposing database using dokku postgres:expose command.

like image 568
Chris Kraszewski Avatar asked Jan 18 '16 09:01

Chris Kraszewski


People also ask

How do I go into a Postgres container?

Connecting to the PSQL server via CLI : Find the docker-container-id in which the postgres is running using the below command. docker ps -a. Run the below command to enter into the container (with the ID from step-1). docker exec -it <PSQL-Container-ID> bash.

How does Docker integrate with PostgreSQL?

Fill the port value as 5432 that runs the Docker PostgreSQL Container and provide the name of the database as postgres. Then, fill the username and password fields with the credentials you created while running the PGAdmin container. After providing all required details, click on the “Save” button.

Where does a Postgres store data in a Docker container?

To circumvent this issue, we can use the information we gathered earlier that showed us that the volume is mounted at /var/lib/postgresql/data. Inside the container, this directory is where Postgres stores all the relevant tables and databases.


1 Answers

To connect with a pgadmin you can get all the necessary information from the DATABASE_URL.

➜  ~ dokku postgres:info your-database-db
       DSN: postgres://postgres:bf8f1cb443c35cd1ae0a58617ef348cd@dokku-postgres-your-database-db:5432/your_database_db

Because dokku have the same foundation as heroku you can extract the information to connect with pg database. The important consideration is that dokku generate a random port for exposure your database. You can get a random port or set any that you want.

[database type]://[username]:[password]@[host]:[port]/[database name]

Exposing the database and getting the real port:

➜  ~ dokku postgres:expose your-database-db
 !     Service is already started
-----> Service your-database-db exposed on port(s) 17825

Now in your pg admin you will add the next information and hopefully connect without issues.

username: postgres
password: bf8f1cb443c35cd1ae0a58617ef348cd
hostname: Your-Dokku-Host-URL (Ex. domain.com or IP of your dokku server)
port: 17825

Also, if you are running dokku in AWS EC2, for example, you need to allow access to this port on the instance, adding a Custom TCP Rule in the Inbound section of the Security Group this instance is associated with.

Hope it helps.

like image 147
Jorge Najera T Avatar answered Sep 19 '22 15:09

Jorge Najera T