superset icon indicating copy to clipboard operation
superset copied to clipboard

Connection failed/refused when connecting to local PostgreSQL Database

Open klazaj opened this issue 6 years ago • 9 comments

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.

klazaj avatar Dec 11 '19 21:12 klazaj

Issue Label Bot is not confident enough to auto-label this issue. See dashboard for more details.

issue-label-bot[bot] avatar Dec 11 '19 21:12 issue-label-bot[bot]

Try postgresql://superset_user:[email protected]:5432/superset (This will force IPv4). Otherwise, are you certain that you can connect to it? psql -h 127.0.0.1 -U superset_user superset

craig-rueda avatar Dec 11 '19 22:12 craig-rueda

@craig-rueda hi, I mention in the end of the question that I tried that host:port. I can also connect to it via psql

klazaj avatar Dec 11 '19 22:12 klazaj

What command are you using to start superset? Is this in docker?

craig-rueda avatar Dec 11 '19 22:12 craig-rueda

@craig-rueda hi, yes I start superset via docker-compose up

klazaj avatar Dec 11 '19 23:12 klazaj

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?

craig-rueda avatar Dec 12 '19 00:12 craig-rueda

@craig-rueda Unfortunately that didn't work for me (Ubuntu 18.04). I haven't used the postgres from the docker-compose stack because I haven't found out if/how it's possible to add a new table to that database programmatically.

klazaj avatar Dec 12 '19 09:12 klazaj

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. For admin, please label this issue .pinned to prevent stale bot from closing the issue.

stale[bot] avatar Feb 10 '20 10:02 stale[bot]

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.

ctivanovich avatar Jul 03 '20 08:07 ctivanovich