1 What command is used to create a new PostgreSQL role
Use CREATE ROLE role_name optionally adding attributes such as LOGIN or CREATEDB
2 How do you create a role that can log in
Use CREATE ROLE role_name LOGIN PASSWORD ‘password’
3 What is the difference between a role and a user in PostgreSQL
There is no functional difference a user is simply a role with the LOGIN privilege
4 How do you list all roles in a PostgreSQL database
Use the psql command \du to display all roles
5 How is a new database created in PostgreSQL
Use CREATE DATABASE db_name
6 How do you assign a database owner at creation time
Use CREATE DATABASE db_name OWNER role_name
7 How do you connect to a specific database using psql
Use \c db_name to switch databases
8 What command deletes a PostgreSQL database
Use DROP DATABASE db_name
9 What is a schema in PostgreSQL
A schema is a logical namespace that organizes database objects
10 How do you create a new schema
Use CREATE SCHEMA schema_name
11 How do you create a table in PostgreSQL
Use CREATE TABLE table_name (column_name data_type)
12 How do you specify a primary key in a table definition
Declare PRIMARY KEY (column_name) or append PRIMARY KEY to the column
13 What is the purpose of the SERIAL data type
It automatically generates sequential integers using a sequence
14 How do you insert a row into a table
Use INSERT INTO table_name (columns) VALUES (values)
15 How do you insert multiple rows in one statement
Use INSERT INTO table_name VALUES (row1)
16 How do you insert data from another table
Use INSERT INTO table_name SELECT columns FROM other_table
17 How do you prevent NULL values in a column
Add the NOT NULL constraint to the column
18 What does the UNIQUE constraint enforce
It ensures all values in a column or column set are distinct
19 How do you define a foreign key
Use FOREIGN KEY (column) REFERENCES other_table(column)
20 How do you grant privileges on a table
Use GRANT privilege ON table_name TO role_name
21 How do you revoke privileges from a role
Use REVOKE privilege ON object FROM role_name
22 What privilege allows a role to create tables in a schema
The CREATE privilege on the schema
23 How do you set a role as the default owner of new objects
Create objects while connected as that role or use ALTER DEFAULT PRIVILEGES
24 How do you change the owner of a table
Use ALTER TABLE table_name OWNER TO role_name