Docker - How can run the psql command in the postgres container?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To run psql inside a Docker PostgreSQL container, use docker exec -it <container> psql -U postgres. This opens an interactive PostgreSQL shell connected to the database running inside the container. You can also run one-off SQL commands, connect from outside the container via exposed ports, or use docker-compose exec for Compose-managed containers.
Start a PostgreSQL Container
--name my-postgres: gives the container a name for easy reference-e POSTGRES_PASSWORD: required, sets the superuser password-e POSTGRES_USER: creates a user (defaults topostgresif omitted)-e POSTGRES_DB: creates a database (defaults to the username if omitted)-p 5432:5432: maps container port to host for external access-d: runs in detached mode
Connect with docker exec
-it: interactive mode with a pseudo-TTY (needed for the psql prompt)psql -U admin: connect as useradmin-d myapp: connect to databasemyapp
You get a full psql prompt:
Run a Single SQL Command
Note: Use -i (not -it) when piping input from a file. The -t flag expects a terminal and conflicts with piped input.
Docker Compose
docker compose exec uses the service name (db) instead of the container name.
Connect from Host Machine
If the port is exposed (-p 5432:5432), connect directly from your host:
This requires psql to be installed on your host machine (via brew install postgresql on macOS or apt install postgresql-client on Ubuntu).
Common psql Commands
Troubleshooting
Importing and Exporting Data
Initialization Scripts
Mount SQL files into /docker-entrypoint-initdb.d/ to run them on first start:
Files in this directory run alphabetically on first container start (when the data directory is empty). Supports .sql, .sql.gz, and .sh files.
Common Pitfalls
- Forgetting
-itflags:docker exec my-postgres psql -U postgreswithout-itgives no interactive prompt. Always use-itfor interactive sessions. - Wrong user: The default superuser is
postgres, notroot. If you setPOSTGRES_USER, use that username with-U. - Container not ready: PostgreSQL takes a few seconds to initialize. If
psqlfails immediately afterdocker run, wait or usepg_isreadyto check. - Using
-itwith piped input:docker exec -it container psql < file.sqlfails because-tconflicts with pipe input. Use-ionly (no-t) for file input. - Port conflict: If port 5432 is already in use on the host (local PostgreSQL), map to a different port:
-p 5433:5432, then connect with-p 5433.
Summary
- Use
docker exec -it <container> psql -U <user>for interactive sessions - Use
-c "SQL"to run single commands without entering the psql shell - Use
docker compose exec <service> psqlfor Compose-managed containers - Expose port 5432 to connect from the host with a local psql client
- Mount init scripts to
/docker-entrypoint-initdb.d/for first-run setup - Use
pg_dumpandpg_restoreviadocker execfor backups

