Invalid username format in Azure database connection

I am unable to connect with database using psycopg2 module. This may be due to the @ character needing escaping, but I have already tried this. Also, it could be related to my password having the "/" character, or the hostname having the "-" character, and I don't know how to escape it (I've already tried escaping using %2f and %2D).

This is the code I have tried with a edited password, so you can see the "special" characters it has, and also the "-" characters in the hostname. Most likely I am doing this wrong.

engine = create_engine('postgresql://username:asd/32foo/T123%[email protected]:5432/database')

The username here is: username,

The password here is: asd/32foo/T123,

The hostame (I think this is) here is: sql-is-a232,

The server here is: sql-is-a232.database.windows.net,

The database name here is: database.

Any help would be greatly appreciated!

Upvotes: 0

Views: 171

Answers (1)

Leon Yue
Leon Yue

Reputation: 16401

Please reference this document: Database Urls:

As the URL is like any other URL, special characters such as those that may be used in the password need to be URL encoded. Below is an example of a URL that includes the password "kx%jj5/g":

postgresql+pg8000://dbuser:kx%25jj5%2Fg@pghost10/appdb

The encoding for the above password can be generated using urllib:

>>> import urllib.parse
>>> urllib.parse.quote_plus("kx%jj5/g")
'kx%25jj5%2Fg'

Examples for common connection styles follow below. For a full index of detailed information on all included dialects as well as links to third-party dialects, see Dialects.

Hope this helps.

Upvotes: 1

Related Questions