First, essential Podman commands you’ll need:
Command | Description |
---|---|
podman ps |
List all running containers |
podman ps -a |
List all containers (including stopped ones) |
podman start <container-name> |
Start an existing container |
podman stop <container-name> |
Stop a running container |
podman rm <container-name> |
Remove a stopped container |
podman logs <container-name> |
View container logs |
podman exec -it <container-name> bash |
Access container’s shell |
podman inspect <container-name> |
View detailed container information |
podman pull postgres:17.4
podman volume create postgres_data
podman run --name postgresql \
-e POSTGRES_USER=root \
-e POSTGRES_PASSWORD=root \
-v postgres_data:/var/lib/postgresql/data \
-p 5432:5432 \
docker.io/library/postgres:17.4
[!Note] If you don’t provide the
--name postgresql
podman will assign a random name, which confused me.3. Managing Your PostgreSQL Container
# Start container
podman start postgresql
# Stop container
podman stop postgresql
# Remove container (if needed)
podman rm postgresql
[!Important] How to get to postgres cli
# Access psql terminal
podman exec -it postgresql psql -U root
Once in psql terminal:
-- List databases
\l
-- Switch to postgres database
\c postgres
-- Create a test table
CREATE TABLE test_table (
id SERIAL PRIMARY KEY,
name VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Exit psql
\q
# List volumes
podman volume ls
# Inspect volume
podman volume inspect postgres_data
# Check volume mounting
podman inspect postgresql
# Access container
podman exec -it postgresql bash
# Create backup
pg_dump -U root -d my_database > /tmp/my_database_backup.sql
# Exit container
exit
# Copy backup to host
podman cp postgresql:/tmp/my_database_backup.sql ./my_database_backup.sql
# Copy backup to container
podman cp ./my_database_backup.sql postgresql:/tmp/my_database_backup.sql
# Restore database
podman exec -it postgresql bash
psql -U root -d my_database < /tmp/my_database_backup.sql
podman run --name postgresql \
--restart=always \
-e POSTGRES_USER=root \
-e POSTGRES_PASSWORD=root \
-v postgres_data:/var/lib/postgresql/data \
-p 5432:5432 \
docker.io/library/postgres:17.4
To use the same volume on different machines:
# Create a tar archive of the volume
podman volume export postgres_data > postgres_data.tar
# Create a new volume
podman volume create postgres_data
# Import the volume data
podman volume import postgres_data postgres_data.tar
# Check port mapping
podman port postgresql
# Access container shell
podman exec -it postgresql bash
# Access psql
psql -U root
# Reset password
ALTER USER root WITH PASSWORD 'new_password';
podman inspect postgresql
podman logs postgresql
to check for volume-related errors