Monday, October 2, 2023

postgres docker linux, configuration

 docker command to create a postgres container with database port exposed and database username set to databaseadmin with admin rights and password set to Product@2020 and volume to set to a local folder for persistance


You can create a PostgreSQL Docker container with the specified settings using the docker run command. Here's a command that accomplishes your requirements:


docker run -d \
  --name postgres-container \
  -e POSTGRES_USER=databaseadmin \
  -e POSTGRES_PASSWORD=Product@2020 \
  -p 5432:5432 \
  -v /path/to/local/folder:/var/lib/postgresql/data \
  postgres:latest

##for specific version use postgres:15

Let's break down what each part of this command does:

  • -d: Runs the container in detached mode (in the background).
  • --name postgres-container: Specifies the name for the container as "postgres-container."
  • -e POSTGRES_USER=databaseadmin: Sets the PostgreSQL username to "databaseadmin."
  • -e POSTGRES_PASSWORD=Erwin@2020: Sets the password for the PostgreSQL user to "Erwin@2020."
  • -p 5432:5432: Maps port 5432 on your local machine to port 5432 inside the container, which is the default PostgreSQL port.
  • -v /path/to/local/folder:/var/lib/postgresql/data: Mounts a local directory (replace /path/to/local/folder with the actual path on your host machine) to the container's /var/lib/postgresql/data directory. This is used for persistent storage of the PostgreSQL data.

Finally, we use the postgres:latest image to create the container. This command will create a PostgreSQL container with the specified configuration, and you can connect to it using the username and password you provided.

To create a database:

# Create an empty database named "licenseanalytics"
docker exec -it postgres-container createdb -U databaseadmin database1


or with container id generated


adminuser@ubuntu-dockertesting:~$ docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS          PORTS                                       NAMES
eda8a415c715   postgres:15   "docker-entrypoint.s…"   20 minutes ago   Up 20 minutes   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   postgres-container

docker exec -it eda8a415c715 createdb -U databaseadmin database1


No comments:

Post a Comment