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

Connection string fails with special characters in password. #20

Open
aviv-skillset opened this issue Apr 10, 2022 · 4 comments
Open

Connection string fails with special characters in password. #20

aviv-skillset opened this issue Apr 10, 2022 · 4 comments

Comments

@aviv-skillset
Copy link

This database server is being used with node-postgres perfectly with same credentials.
on remote server postgresql://myuser:Ex@mp!#[email protected]/mydbname fails
on local docker container postgresql://myuser:administrator@localhost:5431/mydbname works fine
As you can see, the same user and database name is used, yet, when on the local machine when using "administrator" as a password without any special characters, then db connects just fine.

Is there any way to escape special characters?

@aviv-skillset
Copy link
Author

BTW, I would love to know the required PostgreSQL permissions for this utility to work. I can offer donate my skills with documentation and with some guidance maybe even generating some nice warnings for users who lack those permissions.

@danvk
Copy link
Owner

danvk commented Apr 27, 2022

@aviv-skillset can you share the full command that you ran? It may just be a matter of updating from:

pg-to-ts generate -c postgresql://user:pass@host/db -o dbschema.ts

to

pg-to-ts generate -c 'postgresql://user:pass@host/db' -o dbschema.ts

(adding quotes)

@aviv-skillset
Copy link
Author

I believe the problem is deeper than I expected. I've tried to import this function instead of using the CLI binaries.

import { typescriptOfSchema } from 'pg-to-ts';
typescriptOfSchema(`postgresql://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}/${DATABASE_NAME}`)

I have investigated a bit an have seen you are using pg-promise which relies on brianc/node-postgres/pg-connection-string. This package probably is in charge of handling my problem.

I personally connect pg like this

import { DatabaseError, Pool, PoolConfig, QueryConfig } from 'pg';

const poolConfig: PoolConfig = {
  user: DATABASE_USER,
  host: DATABASE_HOST,
  database: DATABASE_NAME,
  password: DATABASE_PASSWORD,
  port: DATABASE_PORT,
};

const pool = new Pool(poolConfig);

and the same "problematic" password worked properly

@danvk
Copy link
Owner

danvk commented Jun 18, 2022

@aviv-skillset looking at the pg-connection-string page that you referenced, you may need to URL encode your password (and possibly the other fields) to make this work. So using your example:

postgresql://myuser:Ex@mp%21%23Passw0rD%[email protected]/mydbname

If this works, I'd very much appreciate a PR to add a note about it to the README. I also think it would be desirable to support passing in connection parameters using environment variables as an alternative to a connection string. (I'd also happily accept a PR for that!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants