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

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

Related

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

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.

Docker Postgres - error while creating a database and user

I use the official Postgres image from the Docker Hub
docker pull postgres
I start my container in my machine on local:
docker run --name some-postgres -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=test postgres
The container would have to create my test base with my user on port 542
I have this error when I want to connect on my db
$ sudo docker run -it postgres /bin/bash
root#ef4407c26a96:/# su postgres
postgres#ef4407c26a96:/$ psql
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
postgres#ef4407c26a96:/$
Please provide the database that you want to connect to. As you've provided POSTGRES_DB=test, use it when you wish to connect to psql:
psql -d test -U user
Also, I was able to connect with psql both as root and postgres user.

psql can not access Postgres running in a Docker container

I have successfully built a postgres-based Docker image that enables PostGIS:
The I run it:
docker run -d -t -p 5432:5432 -v ./data:/data --name postgis-osm-pgrouting -e POSTGRES_PASSWORD=postgres pamtrak06/postgis-pgrouting-osm bash
However, when I try to connect to the database via psql:
psql -h localhost -p 5432 postgres
I get an error:
psql: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
I am a beginner with the port forwarding, but it looks like a port-related issue to me.
Any ideas?
To access an application from within your container you need to first "attach" to that container.
You can do so by running the command:
docker exec -it container_name sh
What this command does is it runs the command sh inside the container container_name
It will prompt a shell terminal where you can now run your psql command like this:
psql -U postgres
Where here you're running psql with the user postgres (default authorized user for psql)
try this
docker run -d -t -p 5432:5432 -v $PWD/data:/data --name postgis-osm-pgrouting -e POSTGRES_PASSWORD=postgres pamtrak06/postgis-pgrouting-osm
and then
psql -h localhost -p 5432 postgres
You've got:
psql: could not connect to server: Connection refused
Is the server running on host "192.168.99.101" and accepting
TCP/IP connections on port 5432?
So apply [Configure PostgreSQL to accept TCP/IP connections][https://www.mozmorris.com/2011/11/15/configure-postgresql-to-accept-tcpip-connections.html], but not in production, for tests purpose only !
And override your Dockerfile with this configuration

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

can't run docker image of jhipster webapp

I have a jhipster monolithic web app with postgress database. I built a docker image using
./gradlew bootRepackage -Pprod buildDocker
Now when I try to run the image using docker run , it fails with following error.
Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:247)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
I tried few things like, but still get the same error:
docker create -v /var/lib/postgresql/data --name spring_app_data postgres:9.5.1
docker run --volumes-from spring_app_data --name spring_app_pg -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -d -P postgres:9.5.1
docker run -it --link spring_app_pg:postgres --rm postgres sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
docker run --name spring_app_container --link spring_app_pg:spring_app_pg -p 8080:8080 -d wmd_server_pg
Any suggestions on how to run the docker image for a webapp with PostgreSQL. BTW I get same kind of error when I use mongodb.
Going by your example commands your database won't be accessible as localhost from the app, it will be via the named container. Configure your apps database connection to use spring_app_pg:5432.
Also, don't use links. Use a user defined network, most likely a bridge is all you will need.
docker network create my_app
docker run --net=my_app --name=spring_app_pg <dbimage>
docker run --net=my_app --name=spring_app_container <appimage>
That should give you the same result as your linked setup.