PostgreSQL Interview Questions

1. What is PostgreSQL?

PostgreSQL is an open-source, advanced relational database management system (RDBMS) known for its extensibility, SQL compliance, and support for both relational and non-relational data.

2. What are some key features of PostgreSQL?

  • ACID compliance
  • Support for JSON and XML data types
  • MVCC (Multi-Version Concurrency Control) for concurrent transactions
  • Full-text search
  • Extensible with custom functions, types, and operators
  • Advanced indexing options (B-tree, GiST, GIN)

3. What is MVCC in PostgreSQL?

MVCC stands for Multi-Version Concurrency Control, a technique PostgreSQL uses to handle concurrent transactions without locking, providing snapshot isolation for readers.

4. How do you create a database in PostgreSQL?

Using SQL command:

CREATE DATABASE dbname;

Or with createdb CLI command.

5. What is a schema in PostgreSQL?

A schema is a namespace within a database that contains tables, views, functions, etc. It helps organize database objects logically.

6. How do you optimize query performance in PostgreSQL?

  • Use indexes
  • Analyze and vacuum tables
  • Use EXPLAIN and EXPLAIN ANALYZE to understand query plans
  • Optimize joins and subqueries
  • Use partitioning for large tables

7. What is the difference between DELETE and TRUNCATE?

  • DELETE removes rows one by one and can be rolled back, fires triggers.
  • TRUNCATE quickly removes all rows in a table without logging individual row deletions, cannot be rolled back if inside a transaction block.

8. What are the types of indexes available in PostgreSQL?

  • B-tree (default)
  • Hash
  • GiST (Generalized Search Tree)
    • BRIN (Block Range Index)

9. Explain the difference between UNION and UNION ALL.

  • UNION removes duplicate rows between result sets.
  • UNION ALL includes all rows including duplicates.

10. What is a CTE (Common Table Expression)?

A CTE is a temporary named result set used within a query, defined using WITH keyword, improving readability and reusability.

11. How do transactions work in PostgreSQL?

Transactions in PostgreSQL follow ACID principles, managed with BEGIN, COMMIT, and ROLLBACK commands, ensuring atomicity and consistency.

12. What is VACUUM in PostgreSQL?

VACUUM reclaims storage by removing dead tuples created by updates and deletes, helping maintain database performance.

13. How do you handle replication in PostgreSQL?

PostgreSQL supports streaming replication (asynchronous/synchronous) and logical replication for data copying between primary and standby servers.

14. What is the difference between logical and physical replication?

  • Physical replication copies the entire database cluster byte-for-byte.
  • Logical replication replicates changes based on database operations (insert/update/delete) and can be selective.

15. How do you manage user permissions in PostgreSQL?

Using GRANT and REVOKE commands to assign or remove privileges on database objects like tables, schemas, and databases.

16. What is the role of pg_hba.conf?

pg_hba.conf (host-based authentication) controls client authentication methods and access rules based on IP, user, and database.

17. What are aggregates in PostgreSQL?

Aggregate functions perform a calculation on a set of values and return a single value, e.g., SUM(), AVG(), COUNT(), MAX(), MIN().

18. How do you perform full-text search in PostgreSQL?

PostgreSQL supports full-text search using tsvector and tsquery types, with functions like to_tsvector() and to_tsquery().

19. What are JSON and JSONB in PostgreSQL?

  • JSON stores JSON data as text.
  • JSONB stores JSON in a binary format allowing faster processing and indexing.

20. How can you back up and restore a PostgreSQL database?

Use pg_dump to export and pg_restore or psql to import backups.