0
0
Node.jsframework~5 mins

PostgreSQL connection with pg in Node.js - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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'?
Aimport { Client } from 'pg';
Bconst Client = require('pg');
Cimport Client from 'pg/client';
Dconst { Client } = require('pg/client');
What is the first step after creating a new Client instance?
ARun client.query() immediately
BCall client.connect() to open the connection
CCall client.end() to close connection
DImport pg again
How do you pass parameters safely to a SQL query using 'pg'?
AUse parameterized queries with placeholders and values array
BConcatenate strings directly
CUse template literals without placeholders
DUse client.query without parameters
What does client.query() return?
ANothing
BA callback function
CA boolean value
DA promise that resolves with query results
Which method closes the database connection?
Aclient.disconnect()
Bclient.close()
Cclient.end()
Dclient.stop()
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.