I am running a postgres db running in Docker with:
docker run -d -p 5432:5432 --name db --env POSTGRES_PASSWORD=postgres --env POSTGRES_USER=postgres postgres
I'm trying to connect Hasura to it, with this command
docker run --name hasura -p 5002:8082 --env HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:postgres#localhost:5432 -e HASURA_GRAPHQL_ENABLE_CONSOLE=true hasura/graphql-engine:latest
but I get this response:
{"type":"startup","timestamp":"2021-04-20T05:49:08.548+0000","level":"info","detail":{"kind":"server_configuration","info":{"live_query_options":{"batch_size":100,"refetch_delay":1},"transaction_isolation":"ISOLATION LEVEL READ COMMITTED","plan_cache_options":{"plan_cache_size":4000},"enabled_log_types":["http-log","websocket-log","startup","webhook-log"],"server_host":"HostAny","enable_allowlist":false,"log_level":"info","auth_hook_mode":null,"use_prepared_statements":true,"unauth_role":null,"stringify_numeric_types":false,"enabled_apis":["metadata","graphql","config","pgdump"],"enable_telemetry":true,"enable_console":true,"auth_hook":null,"jwt_secret":null,"cors_config":{"allowed_origins":"*","disabled":false,"ws_read_cookie":null},"console_assets_dir":null,"admin_secret_set":false,"port":8080}}}
{"type":"startup","timestamp":"2021-04-20T05:49:08.548+0000","level":"info","detail":{"kind":"postgres_connection","info":{"retries":1,"database_url":"postgres://postgres:...#localhost:5432"}}}
{"type":"pg-client","timestamp":"2021-04-20T05:49:08.548+0000","level":"warn","detail":{"message":"postgres connection failed, retrying(0)."}}
{"type":"pg-client","timestamp":"2021-04-20T05:49:08.548+0000","level":"warn","detail":{"message":"postgres connection failed, retrying(1)."}}
{"type":"startup","timestamp":"2021-04-20T05:49:08.548+0000","level":"error","detail":{"kind":"catalog_migrate","info":{"internal":"could not connect to server: Connection refused\n\tIs the server running on host \"localhost\" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432?\n","path":"$","error":"connection error","code":"postgres-error"}}}
{"internal":"could not connect to server: Connection refused\n\tIs the server running on host \"localhost\" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432?\n","path":"$","error":"connection error","code":"postgres-error"}
I am running Docker Desktop v3.3.1 on Windows 10.
Any help on this would be appreciated.
Thanks.
As anemyte mentioned, changing localhost to host.docker.internal works for me.
Related
I'm trying to set up a small postgresql server on my raspberry pi. So far, I've:
Installed postgres onto my pi
Set a static IP adress for my pi
edited /etc/postgresql/13/main/postgresql.conf so that listen_addresses = '*'
edited /etc/postgresql/13/main/pg_hba.conf to include
host all all 0.0.0.0/0 md5
Running pg_isready on my pi gives me
root#pibox:~# pg_isready
/run/postgresql:5432 - accepting connections
However, running the following results in no response
root#pibox:~# pg_isready -d <db_name> -h<static_ip> -p 5432 -U postgres
<pi's IP>:5432 - no response
Additionally, I'm not able to connect from my laptop
psql --host=<static IP> --port=5432 --dbname <db name> --username=postgres
psql: error: connection to server at "<the pi's IP>", port 5432 failed: Connection refused
What are some of the next steps I can take so that I can connect to my postgres server from my laptop and execute queries against the databases?
EDIT:
My laptop is running macOS 12.4. I'm connecting to my pi via ssh over my home network. The pi is running a copy of DietPi. Here is some more relevant info
root#pibox:~# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
Running the following commands yeilds these outputs
root#pibox:~# pg_isready
/run/postgresql:5432 - accepting connections
root#pibox:~# pg_isready -d postgres -h <Pi IP> -p 5432 -U postgres
<Pi IP>:5432 - no response
root#pibox:~# pg_isready -d postgres -h 127.0.0.1 -p 5432 -U postgres
127.0.0.1:5432 - no response
root#pibox:~# pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
13 main 5432 online postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log
I have created database on docker:
docker run --name database -e POSTGRES_PASSWORD=password -d -p 5436:5436 postgres
Created some database inside:
createdb database
Also created superuser. Now I am trying to connect from localhost:
psql -h localhost -p 5436 -U postgres
After that I get following error:
psql: The server closed the connection unexpectedly
This probably means that the server has terminated abnormally
before or while processing your inquiry.
Ports are connected:
In your case, you are trying to expose 5436 and map with your host port 5436, but that might need to use -p 5436:5432 because Postgres uses port 5432 as the default port.
docker run --name database -e POSTGRES_PASSWORD=password -d -p 5436:5432 postgres
There is some description from Container networking
-p 8080:80:Map TCP port 80 in the container to port 8080 on the Docker host.
I need to run several equals Postgres containers on different ports via docker-compose. My problem is that I do not understand how to connect from my terminal to container's psql.
First of all, I should say that my local Postgres is off, so 5432 is free. Also I have listen_addresses = '*' in my containers.
Here is list of my attempts (to make it easier, I've posted examples for one Postgres container):
Run without port specifying
docker-compose.yml:
version: "3"
services:
postgres:
build:
context: ../../
dockerfile: db-load-test/sharding-benchmark/Dockerfile
environment:
- POSTGRES_PASSWORD=postgres
check port:
$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------
sharding-benchmark_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp
try to connect:
$ psql -p 5432 -U postgres
psql: 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"?
$ psql -h localhost -p 5432 -U postgres
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
$ psql -h 0.0.0.0 -p 5432 -U postgres
psql: could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
Specify port and expose it in .yaml (I've changed 5432 to 5433 to make it more expressive)
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------------------------------------
sharding-benchmark_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp, 0.0.0.0:49162->5433/tcp,:::49162->5433/tcp
Similar attempts to connect was unsuccessful. Also it's unclear to my why Postgres still listen 5432:
postgres_1 | 2021-08-02 12:49:01.394 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-08-02 12:49:01.394 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-08-02 12:49:01.397 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Use docker directly
docker run --rm -it -e POSTGRES_PASSWORD=postgres -p 5432:5432/tcp postgres:11
It works with psql -h localhost -p 5432 -U postgres, but if I use docker run with 5432:5433 it breaks, which is unclear to me.
In your docker-compose.yml file, you need to specify ports: for each of the database containers. In each of these the second number must be the normal port for the database (5432); the first number can be any number of your choosing that's not used by another container or host process.
version: '3'
services:
pg1:
image: postgres
# Use default PostgreSQL port 5432 on the host
ports: ['5432:5432']
pg2:
image: postgres
# First port must be different from pg1's above
# Second port must be exactly 5432
ports: ['5433:5432']
When you connect from the host, use a host name of localhost and the first published ports: number. (If you're using the older Docker Toolbox setup, use the docker-machine ip address of the Docker VM; if you're connecting from a different host, use the Docker host's DNS name or IP address.)
# connect to pg1
psql -h localhost -p 5432 -U postgres
# connect to pg2
psql -h localhost -p 5433 -U postgres
You do not need to expose: ports in the docker-compose.yml file; this doesn't really actually do anything. You shouldn't need to reconfigure the stock postgres image beyond its basic environment-variable settings. It doesn't make sense to connect to 0.0.0.0, a special address that means "everywhere" when you're setting up a network listener.
If you're connecting from a different container, none of this matters. Use the Compose service name of the other container (like pg2) and the standard port number (5432). ports: are completely ignored, and localhost means "this container". The two containers must be on the same Docker network; Compose will create a network named default and just using that without explicit networks: settings is fine.
i have been trying out docker container for postgres. So far i have not been able to connect to the database inside the container.
My steps to recreate the problem below.
dockerfile start
FROM postgres
ENV POSTGRES_DB db
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD postgres
COPY db_schema.sql /docker-entrypoint-initdb.d/
I built the docker file like so
$ docker build -t db_con .
And created a container
$ docker run -it -p 5432:5432 --name test_db db_con /bin/bash
View of running container as below
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82347f1114c4 db "docker-entrypoint..." 3 hours ago Up 2 sec 0.0.0.0:5432->5432/tcp test_db
I inspected the container for the address info..
$ docker inspect test_db
--extract start--
"Networks": {
"bridge": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
}
}
--extract end--
Now, i have tried within the container, but i have NOT been successful.
I have tried all the commands below with the error below.
root#82347f1114c4:/# psql -U postgres -h 0.0.0.0 -p 5432 -d db
root#82347f1114c4:/# psql -U postgres -h 172.17.0.1 -p 5432 -d db
root#82347f1114c4:/# psql -U postgres -h 172.17.0.2 -p 5432 -d db
**response for all the above**
psql: could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
I will be delighted if anyone can point me in the right direction. I've hit a wall here, any assistance is much appreciated.
it looks like you override default postgres cmd to /bin/bash.
Why do you put /bin/bash at the end of command?
docker run -it -p 5432:5432 --name test_db db_con /bin/bash
Try to execute
docker run -it -p 5432:5432 --name test_db db_con
Also, postgres will be available only when db dump was restored.
You need to add a new rule in your pg_hba.conf:
nano /etc/postgresql/9.3/main/pg_hba.conf
Add:
host all all [Docker Server IP]/16 md5
Next, you need to uncomment the follow line in the postgres.conf:
listen_addresses = '*' # what IP address(es) to listen on;
Now restart your postgres service, and try again.
I received the following error when trying to write session data using Tomcat's PersistentManager to a Postgres DB running on my local machine:
SEVERE: A SQL exception occurred 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.
The application itself runs in a docker container. For completeness sake, my current context.xml file is:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Manager className="org.apache.catalina.session.PersistentManager"
distributable="true" processExpiresFrequency="6" maxIdleBackup="0" debug="99" >
<Store className="org.apache.catalina.session.JDBCStore"
driverName="org.postgresql.Driver"
connectionURL="jdbc:postgresql://localhost/admin?stringtype=unspecified"
connectionName="admin" connectionPassword="admin"
sessionAppCol="app_name" sessionDataCol="session_data" sessionIdCol="session_id"
sessionLastAccessedCol="last_access" sessionMaxInactiveCol="max_inactive"
sessionTable="tomcat_sessions_tb" sessionValidCol="valid_session" />
</Manager>
</Context>
Pursuant to the suggestions here: Postgresql : Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
I confirmed via a netstat -aln | grep LISTEN that Postgresql is running and listening on the correct ports:
tcp4 0 0 127.0.0.1.5432 *.* LISTEN
tcp6 0 0 ::1.5432 *.* LISTEN
and that my postgresql.conf (located in usr/local/var/postgres) has listen_addresses = localhost and port = 5432, which mirrors the host and port of my running server in Pgadmin3.
I suspect that the problem is that Docker runs in a VM, and thus the local information I have obtained may not be the whole story. Reading up on the available information online, it seems that I may require some sort of bridged networking.
However, I admit I am a novice in this area, and I'm unsure of how to set it up.
Why I can NOT connect to localhost:5432?
Cat your container's /etc/hosts
$ sudo docker exec -it [container] cat /etc/hosts
For docker networks is bridge by default, the localhost inside points to container itself(Docker default bridge network).
Then you don't have 5432 listening in your container:
$ sudo docker exec [container] nc -v -z localhost 5432
Solution 1. If you wanna hardcode the "localhost:5432" inside your config xml, the easiest way is creating your container with the option "--net=host":
$ sudo docker run --net=host -it ...
Solution 2. Change the localhost of your docker host ip inside the container
Get your docker host ip:
$ sudo docker inspect -f '{{ .NetworkSettings.Gateway }}'
192.168.5.1
Enter your container:
$ sudo docker exec -it [container] /bin/bash
Edit the file /etc/hosts to point the localhost to docker host ip:
$ sudo vim /etc/hosts
192.168.5.1 localhost
Solution 3. Modify your db config file to use an alias instead of localhost:
connectionURL="jdbc:postgresql://DB_ALIAS/admin?stringtype=unspecified"
Then add the DB_ALIAS to the container's hosts :
$ sudo docker run --add-host DB_ALIAS:192.168.5.1 -it [image] ...
If you are using docker-compose together with postgres image, than you can reuse service name as IP inside jdbc connection (here: app-db)
web:
build: ./web
ports:
- "8080:8080"
links:
- app-db
environment:
- MYAPP_JDBC_URL=jdbc:postgresql://app-db:5432/somedb
- MYAPP_JDBC_USER=someuser
- MYAPP_JDBC_PASS=pass
app-db:
image: postgres:9.6
environment:
- POSTGRES_USER=someuser
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=somedb
expose:
- 5432
volumes_from:
- app-db-data
app-db-data:
image: cogniteev/echo
command: echo 'Data Container for PostgreSQL'
volumes:
- /opt/postgresdata/:/var/lib/postgresql/data
The best decision!
jdbc:postgresql://host.docker.internal:5432/somedb
Don't thank.
I had to expose port with -p 5432:5432:
docker run --name postgres -e POSTGRES_PASSWORD=secret -d -p 5432:5432 postgres
I was getting the same error but this simple solution works perfect for me.
sudo docker run -d --net="host" -it <IMAGE>
Now I can run my app https://x.x.x.x:pppp/../.. and everything works fine. I hope this helps