0
0
Node.jsframework~10 mins

PostgreSQL connection with pg in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the pg module.

Node.js
const { Client } = require('[1]');
Drag options to blanks, or click blank then click option'
Aexpress
Bpg
Cmysql
Dhttp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'pg'.
Using 'mysql' which is for a different database.
2fill in blank
medium

Complete the code to create a new client with connection details.

Node.js
const client = new Client({ connectionString: '[1]' });
Drag options to blanks, or click blank then click option'
Ahttp://localhost:5432
Bmongodb://localhost:27017
Cpostgresql://user:pass@localhost:5432/mydb
Dftp://localhost:21
Attempts:
3 left
💡 Hint
Common Mistakes
Using HTTP or FTP URLs instead of PostgreSQL connection string.
Omitting username or database name.
3fill in blank
hard

Fix the error in the code to connect the client asynchronously.

Node.js
await client.[1]();
Drag options to blanks, or click blank then click option'
Aconnect
Bstart
Cdisconnect
Dopen
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'disconnect' instead of 'connect'.
Using non-existent methods like 'start' or 'open'.
4fill in blank
hard

Fill both blanks to query the database and get results.

Node.js
const res = await client.[1]('[2]');
Drag options to blanks, or click blank then click option'
Aquery
Bconnect
CSELECT * FROM users
Ddisconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' or 'disconnect' instead of 'query'.
Using invalid SQL commands.
5fill in blank
hard

Fill all three blanks to close the client connection properly.

Node.js
try {
  await client.[1]();
  // do queries
} finally {
  await client.[2]();
  console.[3]('Connection closed');
}
Drag options to blanks, or click blank then click option'
Aconnect
Bend
Clog
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' instead of 'connect'.
Using 'disconnect' before queries.
Using 'console.error' instead of 'console.log'.