Recall & Review
beginner
What is the purpose of the 'pg' library in Node.js?
The 'pg' library allows Node.js applications to connect and interact with PostgreSQL databases easily.
Click to reveal answer
beginner
How do you create a new client connection using 'pg'?
You create a new client by importing { Client } from 'pg' and then creating an instance with connection details like user, host, database, password, and port.
Click to reveal answer
beginner
Why should you call client.connect() before running queries?
Calling client.connect() establishes the connection to the PostgreSQL server so queries can be sent and results received.
Click to reveal answer
beginner
What method do you use to run a SQL query with 'pg' client?
Use client.query(sqlText, values) to run SQL commands. It returns a promise with the query result.
Click to reveal answer
beginner
How do you properly close a PostgreSQL connection using 'pg'?
Call client.end() to close the connection and free resources after finishing database operations.
Click to reveal answer
Which import statement is correct to use the Client class from 'pg'?
✗ Incorrect
The correct modern ES module import is 'import { Client } from 'pg';'.
What is the first step after creating a new Client instance?
✗ Incorrect
You must call client.connect() to establish the connection before running queries.
How do you pass parameters safely to a SQL query using 'pg'?
✗ Incorrect
Parameterized queries prevent SQL injection by using placeholders ($1, $2) and passing values separately.
What does client.query() return?
✗ Incorrect
client.query() returns a promise that resolves to the query result object.
Which method closes the database connection?
✗ Incorrect
client.end() properly closes the connection to the PostgreSQL server.
Explain the steps to connect to a PostgreSQL database using the 'pg' library in Node.js.
Think about setup, connection, querying, and cleanup.
You got /5 concepts.
Describe how to safely run a SQL query with parameters using 'pg'.
Focus on parameterized queries and security.
You got /3 concepts.