20

I am writing a microservice application which has a docker container for postgres database. I know that when dumping SQL to database, we use this docker command in terminal:

cat <dump sql file> | docker exec -i <container ID> psql -U <postgres username> <database name>

I was wondering if there is a similar linux terminal docker command that i can run from outside the container to:

  1. Create database named

  2. Drop database named

Or even:

  1. Delete all tables of the to make it completely empty in one command.

Note that i should be able to run the docker command from outside the container through the host OS terminal (linux).

Any suggestions will be appreciated. Thanks in advance.

3 Answers 3

49

is the Postgres storage local to the container? If so, then removing the container and recreating it will reset your DB in itself. If your data is mounted from a local or network folder, then reseting it means running commands on psql

You have several options:

  • get inside the container with a shell

docker exec -it <container-id> /bin/sh

and then run psql and do whatever you want inside the psql command line.

  • run psql directly in docker

docker exec -it <container-id> psql -U <username> -d <database-name>

  • have psql installed locally and run it to access the postgres instance in docker

psql -U <username> -h localhost

  • run the commands with psql

You cannot DROP and CREATE a database on the same command unfortunately, but you can run 2 separate commands

docker exec -it <container-id> psql -U <username> -d postgres -c "DROP DATABASE <dbname>;"

docker exec -it <container-id> psql -U <username> -d postgres -c "CREATE DATABASE <dbname>;"

Sign up to request clarification or add additional context in comments.

1 Comment

@sshussain270 please change accepted answer :) to this ;)
5

Where your question has cat <dump sql file> you can put anything you want that prints out SQL commands on stdout; that includes echo DROP DATABASE ....

Remember that you can use an ordinary PostgreSQL client to interact with a PostgreSQL server, regardless of whether or not it's running in Docker. (This does require you to have published the database port externally, usually with a docker run -p5432:5432 option or similar.) You do not need root-level access on the host, or a root shell on the database server, just to run database commands.

This shell command will drop a named database on a PostgreSQL instance running on the current host; or if it's not on the current host or on the default port, you can set PGHOST and PGPORT environment variables.

export PGHOST=localhost
# export PGUSER=<postgres username>
# export PGDATABASE=<database name>
echo "DROP DATABASE $1" | psql

Comments

5

A bit ugly, but one liner is also possible :

  1. Find out id of docker container
docker ps | grep postgres | awk '{print $1}'
  1. Paste it in command from post above
docker exec -it $(docker ps | grep postgres | awk '{print $1}') psql -U postgres -d postgres -с $command
  1. We cannot DROP and CREATE a database on the same command, but we can run two times using for loop in bash )
for command in 'delete dbname' 'create dbname'; do echo $command; done
  1. Finally let's compile it together in one liner:
for command in 'drop database metrics;' 'create database metrics;'; do docker exec -it $(docker ps | grep postgres | awk '{print $1}') psql -U postgres -d postgres -c $command; done


PS 'metrics' - database name just an example and you should use yours :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.