node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

How should I create a database? How to inspect query results?

Open myndzi opened this issue 12 years ago • 4 comments

client.query('CREATE DATABASE ?', [ dbName ]) seems to fail; so does $1. I know there used to be an escape function but now I can't find it; the docs have all changed and appear to not include that.

How can I find out what exactly the library is sending to the DB server?

myndzi avatar Jan 13 '14 14:01 myndzi

I think the CREATSE DATABASE command does not support prepared statements.

http://www.postgresql.org/docs/9.1/static/manage-ag-createdb.html

booo avatar Jan 13 '14 14:01 booo

Yeah, that is correct. A rather ... puzzling experience in #postgresql helped clarify that for me, and apparently there is no proper alternate. Next best thing is manual escaping, which I can't seem to find in the docs so... if anybody can point me to the correct location, that'd be nice :)

myndzi avatar Jan 13 '14 14:01 myndzi

I don't think you need to (or can?) escape identifiers, but rather just need to make sure it is a valid identifier and throw an error if not. From the manual:

SQL identifiers and key words must begin with a letter (a-z, but also letters with diacritical marks and non-Latin letters) or an underscore (_). Subsequent characters in an identifier or key word can be letters, underscores, digits (0-9), or dollar signs ($). Note that dollar signs are not allowed in identifiers according to the letter of the SQL standard, so their use might render applications less portable. The SQL standard will not define a key word that contains digits or starts or ends with an underscore, so identifiers of this form are safe against possible conflict with future extensions of the standard.

The system uses no more than NAMEDATALEN-1 bytes of an identifier; longer names can be written in commands, but they will be truncated. By default, NAMEDATALEN is 64 so the maximum identifier length is 63 bytes. If this limit is problematic, it can be raised by changing the NAMEDATALEN constant in src/include/pg_config_manual.h.

benighted avatar Jan 13 '14 19:01 benighted

Ah, right. Well that makes it more straightforward. I also found the function for escaping string literals, so I guess there's no point to this thread! Thanks for your responses.

myndzi avatar Jan 14 '14 05:01 myndzi