I have a server with installed PostgreSQL. All my services work in containers (docker-compose). I want to use my Host PostgreSQL from containers. Buy I have the error:
Unable to obtain Jdbc connection from DataSource (jdbc:postgresql://localhost:5432/shop-bd) for user 'shop-bd-user': Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL State : 08001
Error Code : 0
Message : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection refused (Connection refused)
My docker-compose is using host network_mode, like this:
version: '3'
services:
shop:
container_name: shop
build: ./shop
hostname: shop
restart: always
ports:
- 8084:8084
network_mode: "host"
My database connection URL is: jdbc:postgresql://localhost:5432/shop-bd
Updated Apri, 2018
According to the document, from version 18.03 onwards, using host.docker.internal as DNS works for me, without specifying neither --net=host or network_mode: "host"
This would have worked if you were on linux OS. But this won't work on Mac or Windows. The thing is that when you use --net=host, you still are not on host network. That is the limitation of how docker for windows work
Instead of localhost you need to use docker.for.win.localhost as the host. This is a special DNS name which is available on Docker for Windows when you want to refer to the host localhost
Cautionary note: using the --net=host, or network_mode: "host" will prevent your container from registering with Eureka and will break any kind of API gateway you've got unless they all run on the same network.
If you are mac user, use "host.docker.internal:5432" to connect to localhost Postgres, docs.
1) ensure that your postgresql instance is listening to all addresses and not only localhost.
In postgresql.conf set listen_addresses as follows:
listen_addresses='*'
2) you are trying to connect from your container to localhost:5432 . That is NOT your host's IP address, but the address of your container's loopback device. You need to use your docker bridge IP address instead.
Related
Got a small question here. I suppose I've done something wrong at a moment but i can't find where and it's been +2 hours I'm turning around.
So Basically, I've created a docker-compose with Postgis (Postgres). I wanted to connect on it through Tableplus.
However, I can't ...
2 kind of error keep appearing :
When I try to connect basically on 127.0.0.1, it's keep telling me connection refused
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
When I try to use the docker IPAddress - 172.23.0.2 (docker inspect the image's ID to get the IP address of the image)
could not connect to server: Operation timed out
Is the server running on host "172.23.0.2" and accepting
TCP/IP connections on port 5432?
Here is my docker-compose.yml
version: '3.5'
services:
db:
image: kartoza/postgis:12.1
environment:
- POSTGRES_USER=user1
- POSTGRES_PASSWORD=password1
- POSTGRES_DB=database_db
volumes:
- data_db_volume:/var/lib/postgresql/12
ports:
- "5432:5432"
volumes:
data_db_volume:
At first, when I tried to connect, it was telling me: role user1 doesn't exist.
So to stop this I ran: brew services stop postgresql on my machine
I think a psql was running locally on the same port because with lsof -n -i:5432 | grep LISTEN i keep having information (it stop since I ran stop Postgresql)
Alright so after few days of research and trying on another computer, it seems it was coming from 2 points: the new docker software with a graphic interface that I didn't use before, once this running correctly and a prune done from this place everything started to work fine so I think something was causing error due to an outdated piece of software.
Thank's everyone
I tried opening ports in IPTables,
Set Listen address to *
Added
host all all 23.81.27.0/24 trust
and even
host all all 0.0.0.0/0 trust
YouGetSignal shows the port is closed, and i can't connect to my DB via PgAdmin.
I get
could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "23.81.27.206" and accepting TCP/IP
connections on port 5432?
Any ideas?
I already tried to reboot Postgresql, IPtables, and the server.
With this error
could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "23.81.27.206" and accepting TCP/IP
connections on port 5432?
no need to modify hba_file. Either listen_address in postgres.conf not set to listen on external iface, or firewall blocks it/ not translating/ othernetwork issues. First case often people modify listen_address in wrong postgres.conf or do not restart server. To make sure it did not happen to you, ssh to server, psql to it and check
show config_file;
show listen_addresses;
If those ok, then still on remotes shell :
psql -h 23.81.27.206
If you get connected, stop looking into postgres config - check network (firewall, PAT,NAT,routes, your client connection ((maybe you not connected to the Internet?..)))
Turns out it was IPTables after all. I did "Service iptables stop" And i can connect. I can't manage to allow my ip/open that port. But i guess this is how i have to do it. i just enable it whenever im done.
I'm having issues connecting to my PostgreSQL database from an online source.
PostgreSQL seems to be setup up fine it is running on it's default port 5432 and the postgresql.conf has the following line
listen_addresses = '*'
And the pg_hba.conf has the following
host dbname usname all md5
I can connect to the PostgreSQL database from a different machine using the following credentials in pgadmin
Name local
Host 192.xx.xx.xx
Port 5432
Maintenance DB dbname
Username usname
Password psword
Using these I can connect perfectly it seems to work fine.
Then I forwarded the port 192.xx.xx.xx:5432 on the ADSL router and checked it using the public IP address 197.xx.xx.xx (visible to the internet IP)
http://www.canyouseeme.org/
Where I can see the port was forwarded ok.
However when I try to connect using
Name online
Host 197.xx.xx.xx
Port 5432
Maintenance DB dbname
Username usname
Password psword
I get the server doesn't listen error message
could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "197.xx.xx.xx" and accepting TCP/IP connections on port 5432?
Why is this? Do I need to add an extra step to making it accept connections through port forwarding?
There are similar questions but nothing worked for me. I have already added this line to my pg_hba.conf:
host all all all trust
And this one to my postgresql.conf:
listen_addresses = '*'
My networking settings in the Vagrantfile are:
config.vm.network "private_network", ip: "10.0.0.0"
config.vm.network :forwarded_port, guest: 3000, host: 3000, auto_correct: true
When I try to connect from the host, I get:
$ psql -h 10.0.0.0 -U <username> -d <database>
psql: could not connect to server: Permission denied
Is the server running on host "10.0.0.0" and accepting
TCP/IP connections on port 5432?
The same command from the guest works with no problems. What am I missing??
[UPDATE]
I changed the private network ip to "192.168.1.77" (got it from a working example) and it worked. Still don't know why 10.0.0.0 wasn't good though, since it is in the reserved private address space, so I'll leave the question unanswered.
You need to forward the postgres port, you're only forwarding 3000, not 5432.
I believe the reason you could not connect is that technically 10.0.0.0 is the network address of the range 10.0.0.0/8 (and 10.255.255.255 is the broadcast address and should also not work). The first address of each range is the network address and is not allowed to be a routable host (similarly 192.168.0.0 should fail in the same way since it's a 16-bit block).
In addition to forwarding port 5432 to another port on my host machine in my Vagrantfile, I added the following to my pg_hba.conf file in my vagrant machine, restarted postgresql, and it finally allowed me to connect from the host:
host all all 0.0.0.0/0 md5
I found the suggestion here: https://github.com/laravel/framework/issues/11339
I am trying to connect to Postgresql using the PGAdmin III client in Windows 8.1. Postgresql is installed in a local copy of Vagrant (Ubuntu 14.04) on my Windows machine. It's up and running on Vagrant:
LISTENING
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 814/postgres
VERIFIED USER/PASS/LOCAL CONNECTION
I can access Postgresql locally in Vagrant via SSH in Windows:
vagrant#precise32:/etc/postgresql/9.1/main$ psql -h localhost testdb myuser
Password for user myuser:
psql (9.1.15)
SSL connection (<removed)
Type "help" for help.
testdb=> \quit
PG_HBA.CONF
I added this to my pg_hba.conf file:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all <my IP> md5
VAGRANT CONFIG
My Vagrant config is set to port forward to 5432:
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 5432, host: 15432
POSTGRESQL.CONF
And my postgresql.conf file is set to listen on all IP's:
#listen_addresses = '*' # what IP address(es) to listen on;
PGADMIN ERROR
So, what am I missing here when I try to connect as a guest via PGAdmin to the host and I get the following message, which indicates it sees it but something is not letting me through?:
An error has occurred:
Error connecting to the server: server closed the connection unexpectedly
This probably means the server terminated abnormally before or while processing the request.
PGADMIN CONNECTION INFO
Host: localhost
Port: 15432
Service: <blank>
Maintenance DB: postgres
Username: Myuser (verified)
Password: ****** (verified)
Store password:
Colour: <blank>
Group: Servers
In your Vagrant config add a IP (if not set) eg:
config.vm.network :forwarded_port, host: 15432, guest: 5432
config.vm.network :private_network, ip: "192.168.111.222"
Now from PGAdmin in Windows connect to host 192.168.111.222, port 5432.
Worked for me although I'm not know why.. :P
I dont know what you mean with in pg_hba.conf but in vagrant enviroment you should use ip like 10.0.2.2/24 instead your machine network address.
I had the same problem in linux, and i think in windows this can happen too. In my postgresql.conf the port variable was defined in two places. First with 5432 value, and second with 5435 value.
Running telnet vm_ip 5435, i was able to connect to the server running in the vm.
In my case my vm was running with public_network option, so i have a external ip. In this case, you don't need to forward a port, once you will access the vm,for example, with 192.168.60.15:5435 address.