MCQs on Getting Started with PostgreSQL | PostgreSQL Database

Enhance your PostgreSQL skills with this comprehensive collection of 30 multiple-choice questions. Covering connection methods, basic commands, database creation/deletion, and schema management, these questions are perfect for learners and professionals alike.


Topics Covered:

  • Connecting to PostgreSQL using psql
  • Basic psql Commands
  • Creating and Deleting Databases
  • Understanding Schemas

MCQs on Getting Started with PostgreSQL

Connecting to PostgreSQL using psql

  1. Which command is used to connect to a PostgreSQL database using psql?
    • A) psql -c
    • B) psql -U <username> -d <database>
    • C) connectdb -U <username>
    • D) postgre -U <username>
  2. What is the default port number for PostgreSQL?
    • A) 5432
    • B) 3306
    • C) 1521
    • D) 1526
  3. Which environment variable can be set to store the PostgreSQL username for psql?
    • A) PG_USERNAME
    • B) PGUSER
    • C) PGDBUSER
    • D) USER_PGSQL
  4. How do you connect to a database named testdb as user admin in psql?
    • A) psql –database=testdb –username=admin
    • B) psql -d testdb -U admin
    • C) psql -db testdb –user admin
    • D) connect testdb as admin
  5. If the PostgreSQL server is running on a non-default port, how can it be specified in the psql command?
    • A) psql –port=<port_number>
    • B) psql -p <port_number>
    • C) psql –connect <port_number>
    • D) psql -t <port_number>

Basic psql Commands

  1. Which psql command is used to list all databases?
    • A) \ld
    • B) \dt
    • C) \l
    • D) \db
  2. How can you switch to another database in psql?
    • A) switch <database_name>
    • B) connect <database_name>
    • C) \c <database_name>
    • D) \switch <database_name>
  3. Which psql command is used to display help for SQL commands?
    • A) \h
    • B) \help
    • C) \info
    • D) \commands
  4. What does the \d command do in psql?
    • A) Lists all schemas
    • B) Lists all tables
    • C) Describes the structure of a table
    • D) Deletes a table
  5. How can you exit the psql terminal?
    • A) exit
    • B) quit
    • C) \q
    • D) \exit
  6. Which psql command is used to display all tables in the current database?
    • A) \list-tables
    • B) \d
    • C) \dt
    • D) \table-list
  7. What is the shortcut to run the last executed SQL command again in psql?
    • A) \repeat
    • B) !!
    • C) \g
    • D) !!<command_id>
  8. How do you display the current PostgreSQL version in psql?
    • A) SELECT pg_version();
    • B) SHOW version;
    • C) \v
    • D) SELECT version();
  9. What is the purpose of the \timing command in psql?
    • A) Displays the time taken to execute queries
    • B) Shows server connection time
    • C) Synchronizes server and client time
    • D) None of the above
  10. Which command lists all roles in a PostgreSQL database?
    • A) \roles
    • B) \du
    • C) \list-roles
    • D) SHOW ROLES

Creating and Deleting Databases

  1. Which SQL command is used to create a database in PostgreSQL?
    • A) CREATE DATABASE <db_name>;
    • B) INIT DATABASE <db_name>;
    • C) NEW DATABASE <db_name>;
    • D) DATABASE CREATE <db_name>;
  2. Which role must a user have to create a database in PostgreSQL?
    • A) Superuser
    • B) Database Owner
    • C) Database Administrator
    • D) Creator
  3. How do you delete a database in PostgreSQL?
    • A) DROP DATABASE <db_name>;
    • B) DELETE DATABASE <db_name>;
    • C) REMOVE DATABASE <db_name>;
    • D) CLEAR DATABASE <db_name>;
  4. Which psql command can confirm the successful creation of a database?
    • A) \list
    • B) \db-status
    • C) \show-databases
    • D) SELECT db_name;
  5. Can you delete a database while connected to it in psql?
    • A) Yes
    • B) No
    • C) Only with sufficient privileges
    • D) It depends on the PostgreSQL version

Understanding Schemas

  1. What is the purpose of schemas in PostgreSQL?
    • A) Organize tables and other database objects
    • B) Improve query performance
    • C) Manage database connections
    • D) Define user roles
  2. What is the default schema in a PostgreSQL database?
    • A) default_schema
    • B) base
    • C) public
    • D) root
  3. How do you list all schemas in the current database using psql?
    • A) \ds
    • B) \dt
    • C) \dn
    • D) \schemas
  4. Which command is used to create a new schema in PostgreSQL?
    • A) CREATE NEW SCHEMA <schema_name>;
    • B) NEW SCHEMA <schema_name>;
    • C) CREATE SCHEMA <schema_name>;
    • D) INIT SCHEMA <schema_name>;
  5. What happens if a schema is dropped in PostgreSQL?
    • A) The database is reset
    • B) All objects in the schema are removed
    • C) The schema is archived
    • D) Nothing happens
  6. Can multiple schemas in PostgreSQL contain objects with the same name?
    • A) Yes
    • B) No
    • C) Only for tables
    • D) Only for indexes
  7. Which command is used to delete a schema in PostgreSQL?
    • A) REMOVE SCHEMA <schema_name>;
    • B) DROP SCHEMA <schema_name>;
    • C) DELETE SCHEMA <schema_name>;
    • D) CLEAR SCHEMA <schema_name>;
  8. How can you change the search path for schemas in PostgreSQL?
    • A) SET SCHEMA_PATH ‘<schema>’;
    • B) ALTER SCHEMA SEARCH ‘<schema>’;
    • C) SET search_path TO ‘<schema>’;
    • D) CHANGE SCHEMA ‘<schema>’;
  9. Which system catalog stores information about schemas in PostgreSQL?
    • A) pg_schemas
    • B) pg_namespace
    • C) pg_catalog_schema
    • D) schema_list
  10. How can you rename an existing schema in PostgreSQL?
    • A) RENAME SCHEMA <old_name> TO <new_name>;
    • B) ALTER SCHEMA <old_name> RENAME TO <new_name>;
    • C) UPDATE SCHEMA <old_name> TO <new_name>;
    • D) MODIFY SCHEMA <old_name> TO <new_name>;

Answer Key

QnoAnswer
1B) psql -U <username> -d <database>
2A) 5432
3B) PGUSER
4B) psql -d testdb -U admin
5B) psql -p <port_number>
6C) \l
7C) \c <database_name>
8A) \h
9C) Describes the structure of a table
10C) \q
11C) \dt
12C) \g
13D) SELECT version();
14A) Displays the time taken to execute queries
15B) \du
16A) CREATE DATABASE <db_name>;
17A) Superuser
18A) DROP DATABASE <db_name>;
19A) \list
20B) No
21A) Organize tables and other database objects
22C) public
23C) \dn
24C) CREATE SCHEMA <schema_name>;
25B) All objects in the schema are removed
26A) Yes
27B) DROP SCHEMA <schema_name>;
28C) SET search_path TO ‘<schema>’;
29B) pg_namespace
30B) ALTER SCHEMA <old_name> RENAME TO <new_name>;

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top