Skip to content

Value interpolation fails on CREATE ROLE #1060

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

Open
kodi opened this issue Apr 15, 2025 · 1 comment
Open

Value interpolation fails on CREATE ROLE #1060

kodi opened this issue Apr 15, 2025 · 1 comment

Comments

@kodi
Copy link

kodi commented Apr 15, 2025

Code:

import postgres from "postgres";

const sql = postgres(process.env.DS_DB ?? "", {
    debug: true,
});

const test = async () => {
    const roleName = "test_role";
    const password = "test_password";

    const describeResult = await sql`CREATE ROLE ${sql(
        roleName
    )} WITH LOGIN PASSWORD ${password}`.describe();

    console.log("zz", describeResult);
};

test();

This produces error:

node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

PostgresError: syntax error at or near "$1"
    at ErrorResponse (/<redacted>/node_modules/.pnpm/[email protected]/node_modules/postgres/cjs/src/connection.js:788:26)
    at handle (/<redacted>/node_modules/.pnpm/[email protected]/node_modules/postgres/cjs/src/connection.js:474:6)
    at Socket.data (/<redacted>/node_modules/.pnpm/[email protected]/node_modules/postgres/cjs/src/connection.js:315:9)
    at Socket.emit (node:events:514:28)
    at addChunk (node:internal/streams/readarble:376:12)
    at readableAddChunk (node:internal/streams/readable:349:9)
    at Readable.push (node:internal/streams/readable:286:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at new Query (/<redacted>/node_modules/.pnpm/[email protected]/node_modules/postgres/cjs/src/query.js:35:9)
    at sql (/<redacted>/node_modules/.pnpm/[email protected]/node_modules/postgres/cjs/src/index.js:112:11)
    at test (/<redacted>/lib/dbtest.ts:11:35)
    at /<redacted>/lib/dbtest.ts:18:1
    at evalModule (/<redacted>/node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:1:247313)
    at jiti (/<redacted>/node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:1:245241)
    at Object.<anonymous> (/<redacted>/node_modules/.pnpm/[email protected]/node_modules/jiti/bin/jiti.js:16:1)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47 {
  severity_local: 'ERROR',
  severity: 'ERROR',
  code: '42601',
  position: '45',
  file: 'scan.l',
  line: '1188',
  routine: 'scanner_yyerror',
  query: 'CREATE ROLE "test_role" WITH LOGIN PASSWORD $1',
  parameters: [ 'test_password' ],
  args: [ Identifier { value: '"test_role"' }, 'test_password' ],
  types: [ 0 ]
}

But, if I do it like this:

await sql`CREATE ROLE ${sql(roleName)} WITH LOGIN PASSWORD ${sql.unsafe(`'${password}'`)}`;

then it works and produces valid query ..... LOGIN PASSWORD 'test_password'

Am I doing something wrong?

@cenkce
Copy link

cenkce commented May 6, 2025

The same issue is happening with readable() as well.

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