0
0
Node.jsframework~20 mins

PostgreSQL connection with pg in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PostgreSQL pg Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Identify the correct way to create a PostgreSQL client using pg
Which option correctly creates a new PostgreSQL client instance using the pg library in Node.js?
Aconst pg = require('pg'); const client = new pg.Client({ connectionString: process.env.DATABASE_URL });
Bimport { Client } from 'pg'; const client = new Client(process.env.DATABASE_URL);
Cconst { Client } = require('pg'); const client = new Client({ connectionString: process.env.DATABASE_URL });
Dimport pg from 'pg'; const client = new pg.Client({ connectionString: process.env.DATABASE_URL });
Attempts:
2 left
💡 Hint
Remember that in Node.js with CommonJS, you use require and access Client from the pg module.
component_behavior
intermediate
2:00remaining
What happens after calling client.connect()?
After creating a PostgreSQL client with pg and calling client.connect(), what is the expected behavior?
Node.js
const { Client } = require('pg');
const client = new Client({ connectionString: 'postgresql://user:pass@localhost/db' });
client.connect();
AThe client immediately connects to the database and is ready to run queries synchronously.
BThe client starts an asynchronous connection process; queries can be run after the connection is established.
CThe client throws an error if the connection string is invalid but does not connect otherwise.
DThe client queues queries automatically until the connection is established.
Attempts:
2 left
💡 Hint
Think about how asynchronous operations work in Node.js.
🔧 Debug
advanced
2:00remaining
Why does this pg client connection code throw an error?
Consider this code snippet. Why does it throw an error when trying to connect?
import { Client } from 'pg';
const client = new Client('postgresql://user:pass@localhost/db');
await client.connect();
Node.js
import { Client } from 'pg';
const client = new Client('postgresql://user:pass@localhost/db');
await client.connect();
AThe Client constructor expects an object with a connectionString property, not a string directly.
BThe connection string format is incorrect and causes a parsing error.
CThe await keyword cannot be used outside an async function.
DThe import statement is invalid syntax in Node.js.
Attempts:
2 left
💡 Hint
Check the expected argument type for the Client constructor.
state_output
advanced
2:00remaining
What is the value of result.rows after this query?
Given the following code, what will result.rows contain after the query completes?
const { Client } = require('pg');
const client = new Client({ connectionString: process.env.DATABASE_URL });
await client.connect();
const result = await client.query('SELECT id, name FROM users WHERE id = $1', [42]);
await client.end();
Node.js
const { Client } = require('pg');
const client = new Client({ connectionString: process.env.DATABASE_URL });
await client.connect();
const result = await client.query('SELECT id, name FROM users WHERE id = $1', [42]);
await client.end();
AAn array of all users ignoring the WHERE clause
BUndefined because query returns no rows property
CAn empty array [] if no user with id 42 exists
D[{ id: 42, name: 'Alice' }]
Attempts:
2 left
💡 Hint
Think about what happens if the query finds no matching rows.
🧠 Conceptual
expert
2:00remaining
Which option correctly handles connection errors with pg client?
You want to connect to PostgreSQL using pg and handle connection errors gracefully. Which code snippet correctly does this using async/await?
A
try {
  await client.connect();
} catch (err) {
  console.error('Connection error', err);
  process.exit(1);
}
B
client.connect(err => {
  if (err) throw err;
  console.log('Connected');
});
C
await client.connect().catch(err => {
  console.error('Connection error', err);
  process.exit(1);
});
Dclient.connect().then(() => console.log('Connected')).catch(err => console.error(err));
Attempts:
2 left
💡 Hint
Focus on proper async/await error handling.