Error to query external database in cube.py
- Dominant language
- Rust
- Stars
- 20.8k
- Forks
- 2.1k
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 181
Description
**Problem**
I'm having problem when connecting to an external PostgreSQL database. It works fine in cube.js and the problem was while migrating to cube.py. Users password are stored externally and needs to get credentials using queries.
**Version**
0.35.3
**cube.py**
```py
from cube import config
from sqlalchemy import create_engine, MetaData, Table
@config('check_sql_auth')
def check_sql_auth(req: dict, user_name: str, password: str) -> dict:
engine = create_engine('postgresql://user:password@host:port/database')
print(2)
metadata = MetaData()
print(3)
test_table = Table('users', metadata, autoload_with=engine)
print(4)
```
**docker-compose logs**
```
ubuntu:~/hello-world$ docker-compose -f docker-compose.yml up
Starting hello-world_cubestore_router_1 ... done
Starting hello-world_cubestore_worker_1_1 ... done
Starting hello-world_cubestore_worker_2_1 ... done
Starting hello-world_cube_api_1 ... done
Attaching to hello-world_cubestore_router_1, hello-world_cubestore_worker_2_1, hello-world_cubestore_worker_1_1, hello-world_cube_api_1
cubestore_router_1 | 2024-04-09 20:54:32,067 INFO [cubestored] Cube Store version 0.35.2
cubestore_router_1 | 2024-04-09 20:54:32,074 INFO [cubestore::http::status] Serving status probes at 0.0.0.0:3031
cubestore_router_1 | 2024-04-09 20:54:32,075 INFO [cubestore::metastore::rocks_fs] Using existing metastore in /cube/.cubestore/data/metastore
cubestore_router_1 | 2024-04-09 20:54:32,108 INFO [cubestore::cluster] Meta store port open on 0.0.0.0:9999
cubestore_router_1 | 2024-04-09 20:54:32,108 INFO [cubestore::mysql] MySQL port open on 0.0.0.0:3306
cubestore_router_1 | 2024-04-09 20:54:32,108 INFO [cubestore::http] Http Server is listening on 0.0.0.0:3030
cubestore_worker_1_1 | 2024-04-09 20:54:32,553 INFO [cubestored] Cube Store version 0.35.2
cubestore_worker_2_1 | 2024-04-09 20:54:32,524 INFO [cubestored] Cube Store version 0.35.2
cubestore_worker_2_1 | 2024-04-09 20:54:32,533 INFO [cubestore::cluster] Worker port open on 0.0.0.0:9001
cubestore_worker_1_1 | 2024-04-09 20:54:32,567 INFO [cubestore::cluster] Worker port open on 0.0.0.0:9001
cube_api_1 | 🔗 Cube SQL (pg) is listening on 0.0.0.0:5432
cube_api_1 | 🚀 Cube API server (0.35.3) is listening on 4000
cube_api_1 | 2
cube_api_1 | 3
cube_api_1 | 🔗 Cube SQL (pg) is listening on 0.0.0.0:5432
cube_api_1 | 🚀 Cube API server (0.35.3) is listening on 4000
cubestore_router_1 | 2024-04-09 20:54:47,108 INFO [cubestore::metastore::rocks_fs] Using existing cachestore in /cube/.cubestore/data/cachestore
cubestore_router_1 | 2024-04-09 20:55:47,137 INFO [cubestore::metastore::rocks_store] Persisting cachestore snapshot: done (720.044µs)
```
**requirements.txt**
```
SQLAlchemy==2.0.29
```
**test_connection.py**
```py
>>> import psycopg2
>>> conn = psycopg2.connect( user ="user", password = "password", host = "0.0.0.0", port = 5432, database = "public")
Traceback (most recent call last):
File "", line 1, in
File "/home/ubuntu/.local/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "0.0.0.0", port 5432 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
```
Contributor guide
Assessment
This issue has not been assessed yet.