How to delete all postgresql data from docker including created users and databses? - postgresql

I used to create postgresql database for my old project with command:
docker run --name oldpostgresqldb -e POSTGRES_USER=oldadmin -e POSTGRES_PASSWORD=secret -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres
Then create database with command:
docker exec -i oldpostgresqldb psql -U oldadmin -c "CREATE DATABASE oldDB WITH ENCODING='UTF8' OWNER=oldadmin;"
When I started my new project stoped and removed all containers, images, volumes etc. and even used docker system prune.
Now I am trying to create new container and db with commands:
docker run --name newpostgresqldb -e POSTGRES_USER=newadmin -e POSTGRES_PASSWORD=secret -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres
and
docker exec -i newpostgresqldb psql -U newadmin -c "CREATE DATABASE newDB WITH ENCODING='UTF8' OWNER=newadmin;"
But I receive psql: error: FATAL: role "newadmin" does not exist.
Furthermore, I still can manage oldDB with oldadmin user in newpostgresqldb container, because user oldadmin is still exists.
How can I delete old data and create new user and database using docker?

Thanks to #Ay0 I've found a solution. I just needed to clear content of /data folder in the host directory manualy.

Related

cannot authenticate with postgres

I'm trying to connect to running postgres container with psql:
docker pull postgres
docker run -e POSTGRES_PASSWORD=password -d postgres
C:\Program Files\PostgreSQL\13\bin>psql <myUserName>
Password for user <myUserName>:
at this point I type the given password, in this case just password and get the error
psql: error: FATAL: password authentication failed for user "<myUserName>"
What am I doing incorrectly ?
When you use docker run -e POSTGRES_PASSWORD=password -d postgres, the POSTGRES_PASSWORD would be set for user postgres as default. you can specify your user with POSTGRES_USER environment.
Second thing is that when you run a postgresql container and don't bind any ports for that, you can't connect to that container from outside. So you won't being able to connect to your container with pure pqsl command. Here you have 3 way to connect to your container:
1- Use docker run -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres to run container, then connect to it with psql -h <YOUR_IP> -p 5432 -U <USERNAME>
2- Get your container ip with docker inspect <CONTAINER_NAME> command, then connect to it with psql -h <CONTAINER_IP> -U <USERNAME>
3- Use psql inside your container with docker exec -it <CONTAINER_NAME> psql -U <USERNAME>

Unable to link to a running postgres database docker container and run DDL commands

I am trying to run postgres in a docker container and create an user & database by linking to the running postgress container. Once the user is created I want to provide those details as part of running a Pact brocker instance. However when I run the shell script I am getting error connecting to the postgres container and running ddl queries.
Below are script and error details.
Script
docker run --name pactbroker-db -e POSTGRES_PASSWORD=XXXXX -e POSTGRES_USER=admin -v ~/pact/data:/var/data -d postgres
docker run -it --link pactbroker-db:postgres --rm postgres sh -c 'export PGPASSWORD=XXXXX; exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U admin'
CREATE USER pactbrokeruser WITH PASSWORD 'YYYYY';
CREATE DATABASE pactbroker WITH OWNER pactbrokeruser;
GRANT ALL PRIVILEGES ON DATABASE pactbroker TO pactbrokeruser;
\q
docker run --name pactbroker --link pactbroker-db:postgres -e PACT_BROKER_DATABASE_USERNAME=pactbrokeruser -e PACT_BROKER_DATABASE_PASSWORD=YYYYY -e PACT_BROKER_DATABASE_HOST=postgres -e PACT_BROKER_DATABASE_NAME=pactbroker -d -p 80:80 dius/pact-broker
Error:
./install_pact_postgress.sh
fae1ae3b569a75c33ed5cb7d0faad665c038eb03d9b62c1e2df694f0ce162c5c
psql: error: could not connect to server: could not connect to server: Connection refused
Is the server running on host "XXX.17.0.2" and accepting
TCP/IP connections on port 5432?
./install_pact_postgress.sh: line 6: CREATE: command not found
./install_pact_postgress.sh: line 7: CREATE: command not found
./install_pact_postgress.sh: line 8: GRANT: command not found
./install_pact_postgress.sh: line 10: q: command not found
I am new to docker and not sure where I am going wrong.
Please help.
For database initialization use official postgres docker image feature, which run .sql scripts found in the /docker-entrypoint-initdb.d/ folder
So create your initialization script at your host machine ~/pact/initscriptdir and mount it to that directory starting postrgres.
docker run --name pactbroker-db -e POSTGRES_PASSWORD=XXXXX -e POSTGRES_USER=admin -v ~/pact/initscriptdir:/docker-entrypoint-initdb.d -d postgres

Install Postgis in docker container

I created a database with docker using the postgres image as usual
docker run -d \
--name some-postgres \
-e POSTGRES_PASSWORD=mypassword \
-v ${HOME}/postgres-data/:/var/lib/postgresql/data \
-p 5432:5432 \
postgres
now I decided to add a new column in one of the tables to store coordinates using postgis, however when I do
CREATE EXTENSION postgis;
or something similar I get this error message:
ERROR: could not open extension control file "/usr/share/postgresql/12/extension/postgis.control": No such file or directory
is there an additional step one has to take before running the docker container in order to install postgis?
thanks a lot
The postgis extension does not come with vanilla postgres, which does ship with a whole bunch of more general purpose extensions, though nothing notable for geospatial. Take a look at this instead: https://registry.hub.docker.com/r/postgis/postgis/
I know that there is already a response but this could help someone
Firstly you should already have a Postgres image and after a postgres docker container running .
link 2
After you should have the POSTGIS DOCKER IMAGE
[voir ici] : https://hub.docker.com/r/kartoza/postgis/
The image below shows my config after all these steps
After you should stop the running container of postgres which was running at port 5432 for me
this could help: https://blog.eduonix.com/software-development/learn-stop-kill-clean-docker-containers/#:~:text=To%20stop%20a%20container%20you,the%20killing%20is%2010%20seconds.
Now we can create the container that is going to link our postgres container with our postgis Extension
sudo docker run -d --name postgis_postgres -e POSTGRES_PASSWORD=postgrespassword -e POSTGRES_USER=postgres -v /home/judith/Documents/postgres/db-data/:/var/lib/postgresql/data -p 8000:8000 kartoza/postgis:9.6-2.4
Here /home/judith/Documents/postgres/db-data/ is the path to the database data of postgres container
Now we can enter in the running container created at the step 5 with the command
judith#jlas:~$ sudo docker exec -it postgis_postgres bash
root#544c89fadeda:/# //you will be there
Write the command that is going to link to the postgres console admin , here
the 5432 is port where my postgres container was running and postgres is the admin of my postgres in my config , you will config the database admin in the step 1 do not worry .
root#544c89fadeda:/# psql -h localhost -p 5432 -U postgres
After you can create you POSTGIS EXTENSION
postgres=# CREATE EXTENSION postgis;
CREATE EXTENSION
postgres=#

How to move postgres into docker container?

I usually attach to postgres by typing in the terminal:
psql -U user db
I want to move postgres into docker container:
docker run -v /var/lib/postgresql:/var/lib/postgresql -p 5432:5432 postgres
Then I run command and have an error:
psql -h localhost -p 5432 -U user
psql: FATAL: role "user" does not exist
I expected that -v /var/lib/postgresql:/var/lib/postgresql will make possible to reach db by user. But it doesn't happen. How to make it correctly?

How to create Postgres backups from docker container

I have a Postgres 9.5.4 database running inside a docker container. I am using the official Postgres images and I have been trying to create backups from my database, but not luck so far. According docker documentation I was expecting the following command to store the dump.tar file in the /releases/ folder in the host machine (outside the temporal docker container)
docker run -i --rm --link my_postgres:postgres --volume /releases/:/tmp/ postgres:latest pg_dumpall > /tmp/dump.tar
But this throws the following error:
pg_dumpall: could not connect to database "template1": could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Any idea what could be wrong?
Actually I have found the solution
docker run -i --rm --link my_container:postgres -v /releases/:/tmp/ postgres:latest bash -c 'exec pg_dumpall -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres > /tmp/my_backup.tar'
Where Docker arguments:
-i For interactive processes, like a shell, bash, etc.
--rm For deleting the container once the backup is finished
--link For linking this temporal container to my_container, which contains the currently running Postgres database.
-v /releases/:/tmp/ Creates shared volume. The content inside the folder /tmp/ in the container (In this case my_backup.tar) will be automatically visible on folder /releases/ in the host machine.
And bash arguments:
pg_dumpall To export PostgreSQL cluster into a script file.
-h "$POSTGRES_PORT_5432_TCP_ADDR" Obtains the ip address out of the Postgres variable.
-p "$POSTGRES_PORT_5432_TCP_PORT" Obtains the Port out of the Postgres variable (These variables are defined only in the temporal container we just created, as result of the linkage with my_container)
-U The username for connecting to the database