question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connection failed/refused when connecting to local PostgreSQL Database

See original GitHub issue

I’m trying to connect Superset to my local PostreSQL database via this URI: postgresql://superset_user:password@localhost/superset When I test the connection, I get the following error:

ERROR: {"error": "Connection failed!\n\nThe error message returned was:\n(psycopg2.OperationalError) 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?\ncould not connect to server: Cannot assign requested address\n\tIs the server running on host \"localhost\" (::1) and accepting\n\tTCP/IP connections on port 5432?\n\n(Background on this error at: http://sqlalche.me/e/e3q8)", "stacktrace": "Traceback (most recent call last):\n File \"/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py\", line 2265, in _wrap_pool_connect\n return fn()\n File \"/usr/local/lib/python3.6/site-packages/sqlalchemy/pool/base.py\", line 303, in unique_connection\n return _ConnectionFairy._checkout(self)\n File \"/usr/local/lib/python3.6/site-packages/sqlalchemy/pool/base.py\", line 760, in _checkout\n fairy = _ConnectionRecord.checkout(pool)\n File \"/usr/local/lib/python3.6/site-packages/sqlalchemy/pool/base.py\", line 492, in checkout\n rec = pool._do_get()\n File \"/usr/local/lib/python3.6/site-packages/sqlalchemy/pool/impl.py\", line 238, in _do_get\n return self._create_connection()\n File \"/usr/local/lib/python3.6/site-packages/sqlalchemy/pool/base.py\", line 308, in _create_connection\n return _ConnectionRecord(self)\n File \"/usr/local/lib/python3.6/site-packages/sqlalchemy/pool/base.py\", line 437, in __init__\n self.__connect(first_connect_check=True)\n File \"/usr/local/lib/python3.6/site-packages/sqlalchemy/pool/base.py\", line 639, in __connect\n connection = pool._invoke_creator(self)\n File \"/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py\", line 114, in ...

I tried all the variations of host:port such as 127.0.0.1:5432, but the connection still fails.

The \conninfo output displays You are connected to database "superset" as user "superset_user" via socket in "/var/run/postgresql" at port "5432"., so this should theoretically work.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
craig-ruedacommented, Dec 12, 2019

There’s your problem. In docker’s overlay network, “localhost” will refer to the superset container only. If you want to access postgres on your machine, you will need to connect to host.docker.internal (on Mac). Why not just use the included postgres from the docker-compose stack?

1reaction
ctivanovichcommented, Jul 3, 2020

Just as a note to anyone who stumbles on this, the address for the superset backend db is db:5432 in the SQL URI string for sqlalchemy, and not localhost. I’m not sure why that’s the case, and inspecting the docker network for the precise IP address doesn’t resolve when trying to add the database as a Superset data source. Somewhere during the compose process, I guess superset is configured to see it’s own network at the address of db rather than the IP address(es) of the docker network it’s running on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Postgresql : Connection refused. Check that the hostname ...
Postgresql : Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections [duplicate] · 1...
Read more >
Fix PostgreSQL connection refused easily - Bobcares
In short, several reasons can trigger the PostgreSQL connection refused error. This includes corrupted Postgresql service, firewall restrictions ...
Read more >
PostgreSQL psql: could not connect to server - nixCraft
Step # 1: Allow remote IP address to access PostgreSQL. You need to open file called /var/lib/pgsql/data/pg_hba. · Step # 2: Allow communication ......
Read more >
How To Resolve Or Fix Could Not Connect To Server ...
How To Resolve Or Fix Could Not Connect To Server Connection Refused In PostgreSQL Database pgAdmin How To Resolve Could Not Connect To ......
Read more >
Bitbucket fails to connect to PostgreSQL Server with a ...
Solution · Open the file /var/lib/psql/pg_hba. · You may have to use sudo to obtain the correct permissions to edit and save the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found