Junior — Middle
How to deploy and run PostgreSQL inside a Docker container?
sobes.tech AI
Answer from AI
To deploy and run PostgreSQL in a Docker container, you can use the official PostgreSQL image from Docker Hub.
Example command to start the container:
docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
Explanation:
--name my-postgres— container name.-e POSTGRES_PASSWORD=mysecretpassword— sets the password for thepostgresuser.-p 5432:5432— forwards the port from the container to the host for database access.-d— runs the container in detached mode.
After starting, you can connect to the database at localhost:5432 with the user postgres and the specified password.
For data persistence, it is recommended to use Docker volumes, for example:
docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -v pgdata:/var/lib/postgresql/data -d postgres