danvk/pg-to-ts

Connection string fails with special characters in password.

Opened this issue · 4 comments

This database server is being used with node-postgres perfectly with same credentials.
on remote server postgresql://myuser:Ex@mp!#Passw0rD$@productionhost.com/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?

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 commented

@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)

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 commented

@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%24@productionhost.com/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!)