0

I'm trying to connect to a postgres-DB, which unfortunately has a name with a whitespace in it:

%load_ext sql
from sqlalchemy import create_engine
%sql postgresql://postgres:dbpass@localhost/Test DB

(psycopg2.OperationalError) FATAL:  database "Test DB" does not exist

I've tried to follow some tipps on the internet and used:

import urllib.parse
urllib.parse.quote_plus("Test DB")

which simply results in a string "Test+DB" (this does not work).

How can I adress the database, without changing its name?

Best regards!

2
  • Does //postgres:dbpass@localhost/"Test DB" work? But you should really avoid any identifier (including database names) that require quoting to begin with. Commented Feb 11, 2021 at 7:34
  • No, that also does not work. Yes, I'd never chose to do it that way, but I'm not the owner/admin of the database. Commented Feb 11, 2021 at 7:47

1 Answer 1

1

I was able to solve it by using sqlalchemy's create_engine(), therefore being able to simply save the string (with its whitespace) as a variable (eg database_name):

import sqlalchemy as db
database_name = 'Test DB'
engine = db.create_engine('postgresql://' + 'user_name' + ':' + 'password' + '@localhost/' + database_name)
connection = engine.connect()
s = 'SELECT id FROM user'
df = pd.read_sql_query(s, engine)
df.head()

Hope it helps, best regards.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.