mock can't connect to db - dial tcp 127.0.0.1:5432: connect: connection refused
serpro69 opened this issue · 5 comments
I verified that connection actually works fine:
pg_isready -d postgres -h localhost -p 5432 -U postgres:postgres
localhost:5432 - accepting connections
But mock returns an error:
mock tables -t 'table' -a localhost -d postgres -w password -p 5432 -u postgres
FATA[2023-10-25 14:47:07] Encountered error when connecting to the database, err: dial tcp 127.0.0.1:5432: connect: connection refused
mock tables -t 'table' --uri="postgres://postgres:postgres@localhost:5432/postgres"
FATA[2023-10-25 14:47:19] Encountered error when connecting to the database, err: dial tcp 127.0.0.1:5432: connect: connection refused
@serpro69 , Hello.
- Could you let me know where is this database installed , locally on your machine or via docker?
- Are you using the mock data binary version or the docker version ?
- What OS flavour is your machine mac , linux or windows ?
Thanks
Hi,
Thanks for a quick reply.
- The db runs in a container.
- Mock also runs via container.
- Linux
I think I know the root cause here - two containers can't talk to each other since they don't share a network. Haven't actually thought of that...
I guess if I want to avoid dealing with setting up a "shared network", I'll have to use the binary?
Ya in this case the localhost wont work. Let me spin a docker instance of db and mock and will share the result here
Seems to work for me when both are running in the container, yes you need the communication setup b/w them.
Below is my test, maybe that helps.
-- Pull the postgres db into a container and exposing the port as 5455
% docker run --name mock-postgres-db -p 5455:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -d postgres
Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
a378f10b3218: Already exists
2ebc5690e391: Pull complete
8fe57f734687: Pull complete
a2ddbb09cd9a: Pull complete
5a2499e87ab8: Pull complete
a45f5c4adf1b: Pull complete
178017fd978e: Pull complete
428dff1cb77d: Pull complete
4667364adfc4: Pull complete
4eea1f5281a9: Pull complete
369467411787: Pull complete
51184495a2bc: Pull complete
d3e246f01410: Pull complete
Digest: sha256:3d9ed832906091d609cfd6f283e79492ace01ba15866b21d8a262e8fd1cdfb55
Status: Downloaded newer image for postgres:latest
d97dacaf5a312478c1ebf8ecf890812a12cd829efcd8bb004b72ff58c87605a1
-- Connect to the container and introduce a dummy table
% docker exec -it mock-postgres-db psql -h localhost -U postgres
psql (16.0 (Debian 16.0-1.pgdg120+1))
Type "help" for help.
postgres=# select version();
version
---------------------------------------------------------------------------------------------------------------------
PostgreSQL 16.0 (Debian 16.0-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
(1 row)
postgres=# create table testme ( a int, b int, c text );
CREATE TABLE
postgres=#
-- From the local machine psql client, test the access to the postgres container.
% psql -h localhost -p 5455 -U postgres
Password for user postgres:
psql (14.2, server 16.0 (Debian 16.0-1.pgdg120+1))
WARNING: psql major version 14, server major version 16.
Some psql features might not work.
Type "help" for help.
postgres=# select version();
version
---------------------------------------------------------------------------------------------------------------------
PostgreSQL 16.0 (Debian 16.0-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
(1 row)
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | testme | table | postgres
(1 row)
postgres=# select * from testme;
a | b | c
---+---+---
(0 rows)
postgres=#
-- From the local machine, I can communicate to the database via the port 5455.
-- Get the mock data project onto the container
% docker pull ghcr.io/faisaltheparttimecoder/mock-data:latest
latest: Pulling from faisaltheparttimecoder/mock-data
339de151aab4: Pull complete
7a3f3183aaac: Pull complete
c91c37fe4764: Pull complete
Digest: sha256:b8ad4623beae1ea7d5fe8f075dd04d49f8ae8684b9500703af41b8160a9bae9f
Status: Downloaded newer image for ghcr.io/faisaltheparttimecoder/mock-data:latest
ghcr.io/faisaltheparttimecoder/mock-data:latest
-- Basic docker setup
% docker image tag ghcr.io/faisaltheparttimecoder/mock-data mock
% docker volume create mockdata
mockdata
-- No load the test data to the docker postgres
% docker run -v mockdata:/home/mock mock database -f -u postgres -d postgres -p 5455 -a host.docker.internal -q -i
time="2023-10-25 18:21:33" level=info msg="Version of the database: PostgreSQL 16.0 (Debian 16.0-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit"
time="2023-10-25 18:21:35" level=info msg="The flavour of postgres is: postgres"
time="2023-10-25 18:21:35" level=info msg="The database that will be used by mock program is: postgres"
time="2023-10-25 18:21:35" level=info msg="Starting the program to mock full database"
time="2023-10-25 18:21:35" level=info msg="Extracting the tables in the database: postgres"
time="2023-10-25 18:21:37" level=info msg="Beginning the mocking process for the tables"
time="2023-10-25 18:21:37" level=info msg="Extracting the columns and data type information"
Extracting column information from tables 100% [==================================================] (1/1)
time="2023-10-25 18:21:39" level=info msg="Saving all the backup files to the path: /home/mock/mock/20231025182131"
time="2023-10-25 18:21:49" level=info msg="Total numbers of tables to mock: 1"
Mocking Table "public"."testme" 100% [==================================================] (10/10)
time="2023-10-25 18:21:53" level=info msg="Completed loading mock data to 1 tables"
time="2023-10-25 18:21:53" level=info msg="Successfully completed running the database sub command"
% psql -h localhost -p 5455 -U postgres
Password for user postgres:
psql (14.2, server 16.0 (Debian 16.0-1.pgdg120+1))
WARNING: psql major version 14, server major version 16.
Some psql features might not work.
Type "help" for help.
postgres=# select a,b,substring(c, 1, 10) from testme;
a | b | substring
----------+----------+------------
3772000 | 5828302 | dolorum mo
4454956 | -628709 | est est as
733882 | 1394611 | incidunt s
-6780702 | -5300511 | iste minim
-2977343 | 1032220 | illo beata
1419172 | -3890594 | ad iusto v
6194892 | -7280027 | voluptatum
-1615978 | 1404657 | atque ut s
3608355 | -2389120 | maiores re
5838071 | -952833 | ut et illu
(10 rows)
NOTE: I use "host.docker.internal" for host since I use Mac
For linux, you may connect using the IP found under ifconfig docker0
https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-host-docker-internal
Thanks a lot for the help @faisaltheparttimecoder ! I'll try this out.
I'll close this issue since it's not a bug on mock-data side.