9951 explained code solutions for 126 technologies


postgresqlHow can I use PostgreSQL in a Docker container?


PostgreSQL can be used in a Docker container by following the steps below:

  1. Pull the PostgreSQL Docker image from Docker Hub:
docker pull postgres
  1. Run the Docker container using the image:
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
  1. Connect to the PostgreSQL instance:
docker exec -it some-postgres psql -U postgres
  1. Create a database:
CREATE DATABASE mydb;
  1. Create a user:
CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypassword';
  1. Grant privileges to the user to access the database:
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
  1. Finally, exit the PostgreSQL instance:
\q

Helpful links

Edit this code on GitHub