I have a bare bones postgres container that I can launch no problem using:
docker run -it --rm -v /home/ec2-user/secrets:/mnt/secrets -v demo-postgres:/var/lib/postgresql/data demo-postgres
How do I get to an interactive psql prompt such that i can create a db, add table, values, ect?
I usually separate running container in detached mode and execing into it using psql:
docker run -d -e POSTGRES_PASSWORD=your_password --name pgl postgres:latest
docker exec -it -u postgres pgl psql
Related
I am learning Docker. I have practiced a lot, including testing commands from the official Postgres page on dockerhub.
I ran this command:
docker run -it --rm --network some-network postgres psql -h some-postgres -U postgres
Could someone give a complete and concrete example to make this command work (i mean with a real existing container). I can't see how it could work.
docker run create a docker container
-it create a connection to said container (kinda like TTY) taking in what we write into interactive bash in the container
--rm delete the container when it exit
--network some-network assign some-network network to the container
postgres name of the image
psql -h some-postgres -U postgres connect to PostgreSQL at some-postgres address using postgres user.
Combine the entire command and flags: create a PostgreSQL container and the use the psql command from inside the container to connect to some-postgres using postgres user
For more flags and usage, you can learning from the doc here
Probably, in the Docker hub page is not perfectly clear but your command is used to connect to an already existing Postgres instance.
So, for example, you first create a container with the command:
docker run -it --rm --name postgresql -p 5432:5432 -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin -d postgres:latest
then you can execute your command to connet to it
docker run -it --rm postgres psql -h <your_ip> -U postgres
If your container is running locally, you can get the ip from the bash command ip address
The network attibute is related to the container you first startup so you can decide to leave or remove from the command in relation to the container deploy.
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>
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.
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
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.