Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete postgres database within docker container

I am trying to figure out how to completely remove a docker container with a postgres database and rebuild using docker-compose?

I created a server and database container using docker-compose. The database did not get set up how I wanted, so I would like to remove the database and rebuild. I assumed the easiest solution, given it is brand new would be to stop the container from running, remove the container and then run docker-compose again.

I have followed those steps, do not see any of the containers. I do not see any volumes associated with the containers. However, when I run docker-compose it appears to be using the postgres database that was previously created?

Here is what my docker-compose files consists of with user/password/db name extracted.

services:
  server:
    image: "node:10"
    user: "node"
    working_dir: /home/node/app
    volumes:
      - ./:/home/node/app
    ports:
      - 3030:3030
    command: "npm start"
    depends_on:
      - db
  db:
    image: postgres:latest
    restart: always
    environment:
      POSTGRES_USER: [user] 
      POSTGRES_PASSWORD: [password]
      POSTGRES_DB: [db_name]
    volumes:
      - ./data/postgres:/var/lib/postgresql/data

I expected that by using:

docker stop [container] to stop the container, then docker rm [container] to remove the container

I could rebuild fresh with docker-compose up

like image 644
gditsch Avatar asked Sep 23 '19 16:09

gditsch


People also ask

How do I delete a PostgreSQL database?

The first method to remove a PostgreSQL database is to use the following SQL statement: DROP DATABASE <database name>; The command removes the directory containing the database information and the catalog entries. Only the database owner can execute the DROP DATABASE command.

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.


2 Answers

You can list the volumes used by docker with this command:

docker volume ls

Then, if needed, you can inspect the volumes to find which one your database uses:

docker volume inspect xyzvolumename

After locating the volume used by your database, delete it for a fresh start:

docker volume rm locatedvolumename
like image 137
user3980196 Avatar answered Oct 16 '22 18:10

user3980196


Docker stop and docker rm will not work untill you remove bind mount volume from your docker-compose.

Remove this from your docker-compose

     - ./data/postgres:/var/lib/postgresql/data

or delete everything from host directory inside

./data/postgres
like image 26
Adiii Avatar answered Oct 16 '22 18:10

Adiii