Attempt to connect from SpringBoot to docker-postgres failed - postgresql

I have created a docker container based on a postgres image.
Which I try to connect to from a Spring Boot application with no success.
The user is 'postgres', and the password 'password'.
(I don't have anything from Docker Compose, nor do I know if it's necessary.)
I would appreciate any help

Finally solved:
Remove all previous containers.
Create new container. This time using port 5433 and expecifying the postgres user.
docker run --name container-postgres-1 -p 5433:5432 -v "C:\volumen-docker-1:/var/lib/postgresql/data" -e POSTGRES_PASSWORD=password -e POSTGRES_USER=postgres -d postgres:13.9-alpine3.17
Create the database bootifyone.
docker exec -it container-postgres-1 bash
psql -h localhost -p 5432 -U postgres
CREATE DATABASE bootifyone;
Using this connection URL in the application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5433/bootifyone

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>

Docker container has no IP, but no network flags were set [duplicate]

I have Postgresql on a server in a docker container. How can I connect to it from the outside, that is, from my local computer? What setting should I apply to allow that?
You can run Postgres this way (map a port):
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
So now you have mapped the port 5432 of your container to port 5432 of your server. -p <host_port>:<container_port> .So now your postgres is accessible from your public-server-ip:5432
To test:
Run the postgres database (command above)
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
05b3a3471f6f postgres "/docker-entrypoint.s" 1 seconds ago Up 1 seconds 0.0.0.0:5432->5432/tcp some-postgres
Go inside your container and create a database:
docker exec -it 05b3a3471f6f bash
root#05b3a3471f6f:/# psql -U postgres
postgres-# CREATE DATABASE mytest;
postgres-# \q
Go to your localhost (where you have some tool or the psql client).
psql -h public-ip-server -p 5432 -U postgres
(password mysecretpassword)
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
mytest | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres
So you're accessing the database (which is running in docker on a server) from your localhost.
In this post it's expained in detail.
I managed to get it run on linux
run the docker postgres - make sure the port is published, I use alpine because it's lightweight.
docker run --rm -P -p 127.0.0.1:5432:5432 -e POSTGRES_PASSWORD="1234" --name pg postgres:alpine
using another terminal, access the database from the host using the postgres uri
psql postgresql://postgres:1234#localhost:5432/postgres
for mac users, replace psql with pgcli
You can also access through docker exec command by:
$ docker exec -it postgres-container bash
# su postgres
$ psql
Or
$ docker exec -it postgres-container psql -U postgres
I am using django with postgres in Docker containers. in the docker-compose file, add the following:
db:
image: postgres:10-alpine
environment:
- POSTGRES_DB=app
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=supersecretpassword
ports:
- "6543:5432"
This ports setting uses the port 6543 (it just needs to be different from 5432) that is accessible by your local machine. For myself, I connected DBeaver to it. this will prevent port clashes between your app request and local machine request.
At first, I got a message saying that the port 5432 is in use (which is by django app) so I couldn't access by pgAdmin or DBeaver.
I'm assuming that you want to be able to view data present in your container everytime you connect to it from outside. To do this, you will have to persist data on the postgres image.
If you dont have persistant data, you will have to repeat everything you did the first time.
Steps 3, 5, 6, 7, and 8 answer your question directly.
Here is the detailed overview of the entire process I followed on Windows 10 powershell (commands are the same in Linux and macOS as well):
Step 1: Start powershell in non-admin mode
Step 2: Download postgres docker image:
docker pull postgres:latest
Step 3: Start docker container in detached mode and persist data on postgres image by creating a volume and binding it to a destination
(Note: by default 5432 is the default port that is used; but state it explicitly to prevent connection errors from clients like pgadmin, dbeaver, etc.)
docker run --name postgres-test -e POSTGRES_PASSWORD=password -p 5432:5432 -v postgres-data:/var/lib/postgresql/data -d postgres:latest
Step 4: Check status of running containers
docker ps -a
Step 5: Go inside container_name in interactive mode
(Note: commands like ls, pwd, etc. can be executed here if you've checked linux containers during installation)
docker exec -it postgres-test psql -U postgres
Step 6: Create sample data. At this point, you can play with psql commands in the following manner:
# CREATE DATABASE test;
# \c test
# CREATE TABLE test_table(something int);
# INSERT INTO test_table VALUES (123);
# SELECT * FROM test_table;
# \q
Step 7: Open a database client application like pgadmin or dbeaver and enter the below in the connection fields:
Host: localhost
Database: test
User: postgres
Password: password
Step 8: Enter the query select * from test_table in the query editor and you should be able to see the output 123
I know this is late, if you used docker-compose like #Martin
These are the snippets that helped me connect to psql inside the container
docker-compose run db bash
root#de96f9358b70:/# psql -h db -U root -d postgres_db
I cannot comment because I don't have 50 reputation. So hope this helps.
I already had running postgres on host machine and didn't want to allow connections from network, so I did run temporary postgres instance in container and created database in just two lines:
# Run PostgreSQL
docker run --name postgres-container -e POSTGRES_PASSWORD=password -it -p 5433:5432 postgres
# Create database
docker exec -it postgres-container createdb -U postgres my-db
For some reason 5432 port seems protected. I changed my port config from 5432:5432to 5416:5432 and the following command worked to connect to your postgres database from outside its docker container:
psql -h localhost -p 5416 -U <my-user> -d <my-database>
To connect from the localhost you need to add '--net host':
docker run --name some-postgres --net host -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
You can access the server directly without using exec from your localhost, by using:
psql -h localhost -p 5432 -U postgres
Connect to a local container running postgres
Install psql
brew search postgres
brew install postgresql
2.
docker run --name postgres -e POSTGRES_DB=users \
-e POSTGRES_USER=john \
-e POSTGRES_PASSWORD=password \
-p 5432:5432 -d postgres
psql --host=localhost --username=john --dbname=users
I tried to connect from localhost (mac) to a postgres container. I changed the port in the docker-compose file from 5432 to 3306 and started the container. No idea why I did it :|
Then I tried to connect to postgres via PSequel and adminer and the connection could not be established.
After switching back to port 5432 all works fine.
db:
image: postgres
ports:
- 5432:5432
restart: always
volumes:
- "db_sql:/var/lib/mysql"
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: postgres_db
This was my experience I wanted to share. Perhaps someone can make use of it.
first open the docker image for the postgres
docker exec -it <container_name>
then u will get the root --root#868594e88b53:/#
it need the database connection
psql postgresql://<username>:<databasepassword>#postgres:5432/<database>
This one worked for me:
PGPASSWORD=postgres psql -h localhost -p 3307 -U postgres -d postgres
Use the above to load an initial script as:
PGPASSWORD=postgres psql -h localhost -p 3307 -U postgres -d postgres < src/sql/local/blabla.sql
Do not that i remap my ports as:
docker run -p3307:5432 --name postgres -e POSTGRES_PASSWORD=postgres -d postgres
In case, it is a django backend application, you can do something like this.
docker exec -it container_id python manage.py dbshell
After building my gateway-microservice application i had the same issue. Can not to connect to contenerized postgresql from Heidisql.
At this moment i have solved it by simply specifying postgresql password to docker-compose.yml as well as port.
So you should find and open docker-compose.yml. Then you should enter POSTGRES_PASSWORD (don`t let it to be empty), and specify the port “5432:5432”
services:
microservice33-postgresql:
environment:
- POSTGRES_USER=microservice33
- POSTGRES_PASSWORD=wwww
- POSTGRES_HOST_AUTH_METHOD=trust
ports:
- 5432:5432
link for reference and screenshots post
There are good answers here but If you like to have some interface for postgres database management, you can install pgAdmin on your local computer and connect to the remote machine using its IP and the postgres exposed port (by default 5432).
docker ps -a to get container ids then
docker exec -it psql -U -W

Docker on MacOS, unable to bind Postgres to port 5432

Unsure if this is an issue with Docker for Mac, my Postgres installation (via Homebrew, or some other strange local issue going on.
Probably worth mentioning that I have never ran into this issue on Linux.
Here is a snippet from my docker-compose.yml file that uses the postgres image:
services:
postgres:
image: postgres
environment:
- POSTGRES_DB=some_db
- POSTGRES_USER=some_user
- POSTGRES_PASSWORD=supersecretpassword
ports:
- 5432:5432
After starting compose services docker-compose up, I can see my postgres container running (docker ps).
I can also connect to it directly via docker exec
docker exec -it <container_id> psql -d some_db -U some_user
Everything working as expected so far...
However, when I try to connect to my Docker postgres instance via my local psql client:
psql -h localhost -p 5432 -U some_user -d some_db
It is not connecting to the Docker instance of postgres and rather is trying to use my local postgres instance.
Now the strange part...
When I change the port binding in docker-compose.yml
from 5432:5432
to 5433:5432 (5433 can be any open port)
I am able to connect to the Docker postgres instance as expected via my local psql client:
psql -h localhost -p 5433 -U some_user -d some_db
# After being prompted for my password I'm in!
I don't mind binding to a different port, but I'm still so curious what's happening here!
Anyone know what is happening?
Am I just not able to bind to port 5432?
Is this a Docker for Mac thing?
Is there an issue in the way I installed Postgres?
Thanks!

How can I connect to Postgres database in the container via port 5432

I am running a postgres docker container by using the commands below: (reference: https://docs.docker.com/engine/examples/postgresql_service/)
docker build -t eg_postgresql .
docker run --rm -P --name pg_test eg_postgresql
This works but the port number is dynamic. I can connect to the database by giving the port number. (the port I see in docker ps command)
I would like to connect to this docker database from Python so I need a static port number.
I tried the parameters below:
-p 127.0.0.1:5432:5432
-p 5432:5432
In that case, the docker container's port number was set as 5432. However, I could not connect to the database. I get docker user does not exist error message.
What is your advice?
I took the Dockerfile from the link you posted. After building the container with
docker build -t eg_postgresql .
I started the container with
docker run --rm -p 5432:5432 --name pg_test eg_postgresql (which binds localhost port 5432 to the container port 5432)
and then I tried to connect with
psql -h localhost -p 5432 -d docker -U docker --password
It works like a charm. If you get a message that docker user does not exist please double check that all steps from the Dockerfile are executed succesfully during the docker build command as the creation of the docker user is done in the command RUN /etc/init.d/postgresql start &&\
psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
createdb -O docker docker. Make also sure that you have no PostgreSQL server running on your localhost so that you can be sure that you are trying to connect to PostgreSQL inside the container.

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.