Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to connect to a clickhouse server without default database #207

Closed
v4run opened this issue Jun 22, 2023 · 4 comments · Fixed by #208
Closed

Unable to connect to a clickhouse server without default database #207

v4run opened this issue Jun 22, 2023 · 4 comments · Fixed by #208
Labels
bug Something isn't working

Comments

@v4run
Copy link

v4run commented Jun 22, 2023

Describe the bug

When trying to connect to a clickhouse server without default database, Code: 81. DB::Exception is raised.

Steps to reproduce

  1. Drop database default
  2. Connect to clickhouse server

Expected behaviour

Connection succeeds

Code example

import clickhouse_connect

clickhouse_connect.get_client(host='clickhouse-without-default')

clickhouse-connect and/or ClickHouse server logs

Code: 81. DB::Exception: Database default doesn't exist. (UNKNOWN_DATABASE) (version 22.12.3.5 (official build))

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/clickhouse_connect/__init__.py", line 8, in get_client
    return create_client(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/clickhouse_connect/driver/__init__.py", line 107, in create_client
    return HttpClient(interface, host, port, username, password, database, settings=settings, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/clickhouse_connect/driver/httpclient.py", line 139, in __init__
    super().__init__(database=database,
  File "/usr/local/lib/python3.11/site-packages/clickhouse_connect/driver/client.py", line 68, in __init__
    server_settings = self.query(f'SELECT name, value, {readonly} as readonly FROM system.settings LIMIT 10000')
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/clickhouse_connect/driver/client.py", line 188, in query
    return self._query_with_context(query_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/clickhouse_connect/driver/httpclient.py", line 208, in _query_with_context
    response = self._raw_request(body,
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/clickhouse_connect/driver/httpclient.py", line 407, in _raw_request
    self._error_handler(response)
  File "/usr/local/lib/python3.11/site-packages/clickhouse_connect/driver/httpclient.py", line 332, in _error_handler
    raise OperationalError(err_str) if retried else DatabaseError(err_str) from None
clickhouse_connect.driver.exceptions.DatabaseError: :HTTPDriver for http://clickhouse:8123 returned response code 404)
 Code: 81. DB::Exception: Database default doesn't exist. (UNKNOWN_DATABASE) (version 22.12.3.5 (official build))

Configuration

Environment

  • clickhouse-connect version: 0.6.3
  • Python version: 3.11.4
  • Operating system: Debian GNU/Linux 12 (bookworm) (docker container)

ClickHouse server

  • ClickHouse Server version: 22.12.3.5

Sample docker-compose.yaml file to recreate the issue

version: "3"
services:
  clickhouse:
    image: clickhouse/clickhouse-server:22.12.3.5

  job:
    image: python:3
    entrypoint: ["bash", "-c"]
    depends_on:
      - clickhouse
    command: |
      "pip install clickhouse-connect
       until [ \"$$STATUS\" = \"Ok.\" ]; do STATUS=$(curl -s 'http://clickhouse:8123'); sleep 2; done
       echo 'DROP DATABASE default' | curl -s 'http://clickhouse:8123' --data-binary @-
       python3 -c \"import clickhouse_connect; clickhouse_connect.get_client(host='clickhouse', database='system')\""
@v4run v4run added the bug Something isn't working label Jun 22, 2023
@genzgd
Copy link
Collaborator

genzgd commented Jun 22, 2023

Does it work if you set the database argument to a valid database or None?

@v4run
Copy link
Author

v4run commented Jun 22, 2023

Nope. I tried setting it to 'system' and None.

@genzgd
Copy link
Collaborator

genzgd commented Jun 22, 2023

So the problem is the ClickHouse function currentDatabase() returns default even if the default database doesn't exist. I'll have to work around that behavior on initialization.

@genzgd genzgd linked a pull request Jun 22, 2023 that will close this issue
2 tasks
@pkit
Copy link

pkit commented Jun 27, 2023

So the problem is the ClickHouse function currentDatabase() returns default even if the default database doesn't exist.

No, the problem is that @v4run didn't set default_database in the server settings after dropping default database. And did not restart the server.
I.e. it's not a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants