I'm trying to access PostgreSQL's shell (psql
) using docker-compose
, but I'm having some difficulties... Here's my docker-compose
file:
main:
build: .
volumes:
- .:/code
links:
- postgresdb
environment:
- DEBUG=true
postgresdb:
build: utils/sql/
ports:
- "5432"
environment:
- DEBUG=true
I've tried to access psql
by going through the main
as well as the postgresdb
services, by running
docker-compose run postgresdb psql -h postgresdb -U docker mydatabase
but all I get is psql: could not translate host name "postgresdb" to address: Name or service not known
... which I don't understand because I use postgresdb
as the host in the database configuration file e.g.:
DB_ACCESS = {
'drivername': 'postgres',
# Name of docker-compose service
'host': 'postgresdb',
'port': '5432',
'username': 'docker',
'password': '',
'database': 'mydatabase'
}
Connecting to a Local Database Using psqlServer [localhost]: Database [postgres]: Port [5432]: Username [postgres]: Password for user postgres: If you simply press Enter without entering any variables, they will be filled in automatically with the default values.
Connecting to the PSQL server via CLI : Run the below command to enter into the container (with the ID from step-1). docker exec -it <PSQL-Container-ID> bash. Authenticate to start using as postgres user. psql -h localhost -p 5432 -U postgres -W.
You can use Docker Compose to configure the Postgres as a service running inside of a container. In that case, you create a yaml file with all the specifications.
Things have changed a bit since the question was originally asked.
Here's how you can access PostgreSQL's shell (psql) using docker-compose today:
docker-compoose.yml
version: '2'
services:
postgresdb:
build: utils/sql/
ports:
- "5432"
environment:
- DEBUG=true
main:
build: .
volumes:
- .:/code
depends_on:
- postgresdb
environment:
- DEBUG=true
In your docker engine shell ...
docker ps
commandSee example below:
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.11.2, build HEAD : a6645c3 - Wed Jun 1 22:59:51 UTC 2016
Docker version 1.11.2, build b9f10c9
docker@default:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b563764171e2 poc_web "bundle exec rails s " 43 minutes ago Up 43 minutes 0.0.0.0:3000->3000/tcp poc_web_1
c6e480b0f26a postgres "/docker-entrypoint.s" 49 minutes ago Up 49 minutes 0.0.0.0:5432->5432/tcp poc_db_1
docker@default:~$ docker exec -it c6e480b0f26a sh
# su - postgres
No directory, logging in with HOME=/
$ psql
psql (9.5.3)
Type "help" for help.
postgres=#
Notes:
If you want to stand up a postgres container using the official image, you docker-compose.yml might look more like the following:
version: '2'
services:
db:
image: postgres
ports:
- "5432:5432"
. . .
References
https://docs.docker.com/compose/compose-file/
Let's start with a minimal docker-compose.yml
file for reference:
version: "3"
services:
postgres:
image: postgres:9.5
You bring your services up with docker-compose up
.
Let's assume you have a database you want to connect to called test
.
Answer: docker-compose run --rm postgres psql -h YOUR_SERVICE -U YOUR_USER -d YOUR_DATABASE
The --rm
option removes the container after exit, so you don't create unnecessary duplicate containers just for accessing the database.
In our example, this would be docker-compose run --rm postgres psql -h postgres -U postgres -d test
Alternatively, you could use psql's connection string: docker compose run --rm postgres psql -d postgres://YOUR_USER@YOUR_SERVICE/YOUR_DATABASE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With