How to Drop Databases in Redis, MongoDB, and PostgreSQL
📚 How to Check and Delete Redis and MongoDB Data Using Docker Compose
This tutorial will guide you through the process of checking if a key exists in a Redis database, deleting it if necessary, and also how to delete data from MongoDB, all using Docker Compose commands.
🛠️ Prerequisites
- Docker and Docker Compose installed
- Redis and MongoDB services defined in your
docker-compose.ymlfile
🚀 Using Docker Compose
🔴 Interacting with Redis
🔍 Check if a Key Exists
-
Ensure your Redis container is running by using Docker Compose:
docker-compose up -d -
Connect to the Redis container using the following command:
docker-compose exec redis redis-cliReplace
rediswith the name of your Redis service defined in yourdocker-compose.ymlfile. -
Check if the key exists using the
EXISTScommand:EXISTS your-key-nameReplace
your-key-namewith the actual key you want to check. The command will return1if the key exists and0if it does not.
🗝️ Check All Keys
- List all keys using the
KEYScommand:This command will return a list of all keys stored in the currently selected database.KEYS *
📝 Get Key Information
- Get detailed information about a specific key using the
TYPEandTTLcommands:TYPE your-key-name TTL your-key-nameTYPEreturns the data type of the key (e.g., string, list, set).TTLreturns the remaining time to live of a key that has a timeout, or-1if the key does not have a timeout.
🗑️ Delete a Specific Key
- Delete the key using the
DELcommand:DEL your-key-name
🧹 Delete All Keys
-
Delete all keys in the currently selected database using the
FLUSHDBcommand:FLUSHDBThis will remove all keys from the currently selected database.
-
Delete all keys in all databases using the
FLUSHALLcommand:FLUSHALLThis will remove all keys from all databases.
🟢 Interacting with MongoDB
🔎 Determine the Actual Name of Your Database
-
Ensure your MongoDB container is running by using Docker Compose:
docker-compose up -d -
Connect to the MongoDB container using the following command:
docker-compose exec mongo mongoReplace
mongowith the name of your MongoDB service defined in yourdocker-compose.ymlfile. -
List all databases:
show dbsThis command will display a list of all databases on your MongoDB server along with their sizes.
Example Output:
> show dbs admin 0.000GB config 0.000GB local 0.000GB exampleDB 0.001GBIn this example,
exampleDBis the name of one of the databases.
📂 Select and Work with a Database
Once you know the name of your database, you can select it using the use command:
- Select the database:
Replace
use exampleDBexampleDBwith the actual name of your database.
📋 List Collections in a Database
After selecting the database, you can list all collections within it:
-
List all collections:
show collectionsThis will display all collections within the selected database.
Example Output:
users orders products
🗑️ Delete All Collections or Specific Collections
🗑️ Delete All Collections
- Drop the entire database (this will delete all collections and documents within it):
This command will remove the entire database, including all collections and their documents.
db.dropDatabase()
🗑️ Delete Specific Collections
- Drop a specific collection:
Replace `your-colle…
db.your-collection-name.drop()
Guide to Manage PostgreSQL Databases in Docker 🐘
This guide will help you list PostgreSQL users and databases, find your database name, and drop a PostgreSQL database within a Docker container.
Listing PostgreSQL Users and Databases
Step 1: Access the PostgreSQL Container
To access your running PostgreSQL container, use the following command. Replace your_container_name with the actual name or ID of your container.
docker exec -it your_container_name bash
Step 2: Connect to PostgreSQL
Once inside the container, connect to the PostgreSQL database using the psql command. Replace username with your PostgreSQL username (often postgres).
psql -U username
Step 3: List Users
To list all users, execute:
\du
Step 4: List All Databases
To list all databases, use:
\l
Step 5: Find Your Database Name
If you’re not sure about your database name, you can check the list of databases using the command above (\l). Look for the name in the output.
Step 6: Exit
To exit the psql prompt, type:
\q
Then, exit the container:
exit
Dropping a PostgreSQL Database 💔
Step 1: Access the PostgreSQL Container
If you are not already inside the container, access it using the same command as before:
docker exec -it your_container_name bash
Step 2: Connect to PostgreSQL
Connect to PostgreSQL using:
psql -U username
Step 3: Drop the Database
After connecting, you can drop the database by executing the following command. Replace zilean with the name of the database you want to drop.
DROP DATABASE zilean;
Step 4: Exit
To exit the psql prompt, type:
\q
Then, exit the container:
exit
Summary 🌟
This guide provided instructions on how to list PostgreSQL users and databases, find your database name, and drop a PostgreSQL database within a Docker container. Ensure you have the necessary permissions and always back up your data before performing destructive operations.