2

I'm cant connect to PostgreSQL with php code. My Postgresql is docker container. I get an IP with:

docker inspect toshi_db_1

How I'm trying to connect:

$dbconn = pg_connect("host=172.17.0.2 port=5432 dbname=toshi_development")or die("Could not connect");

Error: Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection timed out (0x0000274C/10060) Is the server running on host "172.17.0.2" and accepting TCP/IP connections on port 5432?

I thought there could be problems in PostgreSQL.conf with listen_address configuration parameter, but it allows all connections, so I have no idea where the problem is.

UPDATE: I fixed it myself, just tried to expose wrong port in docker-compose file.

9
  • Did you expose the port correctly when creating the container? Are you using docker-composer? Commented Feb 1, 2017 at 12:50
  • I'm using docker-composer Commented Feb 1, 2017 at 12:58
  • Are you exposing the port 5432 correctly? Show us your 'docker-compose.yml` file. Commented Feb 1, 2017 at 14:43
  • web: &web build: . links: - db - redis ports: - "5000:5000" environment: TOSHI_ENV: development command: bundle exec puma blockworker: <<: *web command: bundle exec sidekiq -q blocks -c 1 -r ./config/environment.rb ports: [] transactionworker: <<: *web command: bundle exec sidekiq -q transactions -c 1 -r ./config/environment.rb ports: [] peermanager: <<: *web ports: [] command: bundle exec ruby bin/peer_manager.rb db: image: "postgres:9.3" redis: image: "redis:2.8" Commented Feb 1, 2017 at 14:45
  • I'm trying to run toshi block explorer, but all I need is to connect to postgres database through php code. I cannot ping that ip from host... Commented Feb 1, 2017 at 14:49

1 Answer 1

7

I run in the same situation today. The solution was replacing the localhost port with the name of the container. Like:

$dbconn = pg_connect("host=toshi_db_1 port=5432 dbname=toshi_development")or die("Could not connect");

Not sure if it should be the same with IPs. But doesn't the IPs change?

So, for other people seeing this post: remember that 127.0.0.1 always points to current container. So you must replace that "localhost" part with your container name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.