pg-to-ts icon indicating copy to clipboard operation
pg-to-ts copied to clipboard

Connection string fails with special characters in password.

Open aviv-skillset opened this issue 2 years ago • 4 comments

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 avatar Apr 10 '22 12:04 aviv-skillset

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.

aviv-skillset avatar Apr 18 '22 17:04 aviv-skillset

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

danvk avatar Apr 27 '22 16:04 danvk

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

aviv-skillset avatar May 19 '22 10:05 aviv-skillset

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

danvk avatar Jun 18 '22 14:06 danvk