Programming & Development / March 27, 2025

How to Select Data from a Database Inside Docker (MySQL & PostgreSQL Examples)

docker select table docker database query mysql in docker postgres in docker docker exec sql connect to mysql docker connect to postgres docker psql docker mysql client docker docker-compose mysql postgres

Learn how to access and query a database running inside a Docker container. This article covers:

  • Setting up MySQL or PostgreSQL in Docker
  • Connecting from the host or using docker exec
  • Selecting data from a table using SQL
  • Using Docker Compose for simplified setups

Ideal for developers working with Dockerized databases in local development environments.

🐬 Part 1: Accessing a MySQL Container and Running SELECT Queries

✅ Step 1: Start a MySQL Container

bash

docker run --name mysql-container \
  -e MYSQL_ROOT_PASSWORD=rootpassword \
  -e MYSQL_DATABASE=mydatabase \
  -e MYSQL_USER=myuser \
  -e MYSQL_PASSWORD=mypassword \
  -p 3306:3306 -d mysql:latest

✅ Step 2: Install MySQL Client (If Needed)

Ubuntu:

bash

sudo apt-get update
sudo apt-get install mysql-client

✅ Step 3: Connect from Host

bash

mysql -h 127.0.0.1 -P 3306 -u myuser -p mydatabase

(Then enter the password mypassword)

✅ Step 4: Query the Table

sql

SELECT * FROM mytable;

✅ Optional: Run SQL from Inside the Container

bash

docker exec -it mysql-container mysql -u myuser -pmypassword mydatabase

Then:

sql

SELECT * FROM mytable;

🐘 Part 2: Accessing a PostgreSQL Container and Running SELECT Queries

✅ Step 1: Start a PostgreSQL Container

bash

docker run --name postgres-container \
  -e POSTGRES_PASSWORD=mysecretpassword \
  -e POSTGRES_DB=mydatabase \
  -e POSTGRES_USER=myuser \
  -p 5432:5432 -d postgres:latest

✅ Step 2: Install PostgreSQL Client (If Needed)

Ubuntu:

bash

sudo apt-get update
sudo apt-get install postgresql-client

✅ Step 3: Connect from Host

bash

psql -h 127.0.0.1 -p 5432 -U myuser -d mydatabase

(Then enter password mysecretpassword)

✅ Step 4: Query the Table

sql

SELECT * FROM mytable;

✅ Optional: Run SQL from Inside the Container

bash

docker exec -it postgres-container psql -U myuser -d mydatabase

Then:

sql

SELECT * FROM mytable;

📦 Optional: Docker Compose Setup for MySQL/PostgreSQL + Adminer

yaml

version: '3.1'

services:

  db:
    image: mysql:latest # Or postgres:latest
    container_name: db-container
    environment:
      MYSQL_ROOT_PASSWORD: rootpassword
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypassword
      # For PostgreSQL use:
      # POSTGRES_DB: mydatabase
      # POSTGRES_USER: myuser
      # POSTGRES_PASSWORD: mysecretpassword
    ports:
      - "3306:3306" # or "5432:5432"

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

Start with:

bash

docker-compose up -d

Access Adminer at http://localhost:8080

📌 Summary Table

TaskMySQL CommandPostgreSQL CommandStart Containerdocker run ... mysqldocker run ... postgresConnect from Hostmysql -h 127.0.0.1 -P 3306 -u myuser -p mydbpsql -h 127.0.0.1 -p 5432 -U myuser -d mydbConnect from Containerdocker exec -it mysql-container mysql ...docker exec -it postgres-container psql ...Run SQLSELECT * FROM mytable;SELECT * FROM mytable;Web Admin Tool (optional)Adminer at http://localhost:8080Adminer at http://localhost:8080


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex