<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String address;
//getters and setters
}
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByAddress(String address);
}
@Service
public class UserService {
@Autowired
private UserRepository repository;
public User addUser(User user) {
return repository.save(user);
}
public List<User> getUsers() {
return repository.findAll();
}
public List<User> getUserbyAddress(String address) {
return repository.findByAddress(address);
}
public void deleteUser(User user) {
repository.delete(user);
}
}
@RestController
public class UserController {
@Autowired
private UserService service;
@PostMapping(value = "/save")
public User saveUser(@RequestBody User user) {
return service.addUser(user);
}
@GetMapping("/getUsers")
public List<User> findAllUsers() {
return service.getUsers();
}
@GetMapping("/getUserByAddress/{address}")
public List<User> findUserByAddress(@PathVariable String address) {
return service.getUserbyAddress(address);
}
@DeleteMapping(value="/remove")
public User removeUser(@RequestBody User user) {
service.deleteUser(user);
return user;
}
}
spring.datasource.url=jdbc:postgresql://host:port/dbname
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.javatechie.spring.postgres.api.model.Employee;
import com.javatechie.spring.postgres.api.repository.EmployeeRepository;
import com.javatechie.spring.postgres.api.service.EmployeeService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmployeeServiceTest {
@Autowired
private EmployeeService service;
@Autowired
private EmployeeRepository repository;
@Test
public void testSaveEmployee() {
Employee employee = new Employee(0, "John Doe", "Software Engineer");
Employee saved = service.saveEmployee(employee);
assertThat(saved.getId()).isGreaterThan(0);
assertThat(saved.getName()).isEqualTo("John Doe");
assertThat(saved.getDesignation()).isEqualTo("Software Engineer");
}
@Test
public void testGetEmployees() {
List<Employee> employees = service.getEmployees();
assertThat(employees).isNotEmpty();
assertThat(employees.size()).isEqualTo(repository.findAll().size());
}
@Test
public void testGetEmployeeById() {
Employee employee = service.getEmployeeById(1);
assertThat(employee.getId()).isEqualTo(1);
assertThat(employee.getName()).isEqualTo("Jane Doe");
assertThat(employee.getDesignation()).isEqualTo("Software Developer");
}
@Test
public void testDeleteEmployee() {
service.deleteEmployee(1);
assertThat(repository.findById(1).isPresent()).isFalse();
}
}
Preparing for a PostgreSQL interview involves understanding both basic and advanced concepts, as well as practical skills. Here are some common PostgreSQL interview questions along with brief explanations to help you prepare:
-
What is PostgreSQL?
- Answer: PostgreSQL is an open-source, object-relational database management system (ORDBMS) known for its robustness, extensibility, and compliance with SQL standards.
-
What are some key features of PostgreSQL?
- Answer: ACID compliance, support for advanced data types (JSON, arrays, hstore), full-text search, indexing, extensibility (custom functions, data types), MVCC (Multi-Version Concurrency Control), and strong support for procedural languages (PL/pgSQL, PL/Python).
-
What is MVCC in PostgreSQL?
- Answer: Multi-Version Concurrency Control (MVCC) is a method used to handle concurrent transactions without locking the database. It allows readers to see a consistent snapshot of the database, ensuring isolation and improving performance.
-
What are tablespaces in PostgreSQL?
- Answer: Tablespaces are storage locations on disk where PostgreSQL stores database objects. They allow administrators to control the physical location of data and can be used to optimize performance and manage disk space.
-
How do you create a database in PostgreSQL?
- Answer:
CREATE DATABASE database_name;
- Answer:
-
What is the difference between
CHAR,VARCHAR, andTEXTdata types in PostgreSQL?- Answer:
CHAR(n)is a fixed-length character type,VARCHAR(n)is a variable-length character type with a limit, andTEXTis a variable-length character type without a specific length limit.
- Answer:
-
How do you back up a PostgreSQL database?
- Answer: Use the
pg_dumputility for logical backups orpg_basebackupfor physical backups. Example:pg_dump -U username -F c database_name > backup_file.dump
- Answer: Use the
-
How do you restore a PostgreSQL database from a backup?
- Answer: Use the
pg_restoreutility for restoring from a custom-format dump. Example:pg_restore -U username -d database_name -1 backup_file.dump
- Answer: Use the
-
What are the different types of indexes in PostgreSQL?
- Answer: B-tree, Hash, GiST, SP-GiST, GIN, and BRIN indexes. Each type is optimized for different types of queries and data structures.
-
How do you create and use a composite primary key in PostgreSQL?
- Answer:
CREATE TABLE example ( column1 datatype, column2 datatype, PRIMARY KEY (column1, column2) );
- Answer:
-
What is a CTE (Common Table Expression)?
- Answer: A CTE is a temporary result set that you can reference within a
SELECT,INSERT,UPDATE, orDELETEstatement. It is defined using theWITHkeyword.
- Answer: A CTE is a temporary result set that you can reference within a
-
How do you handle JSON data in PostgreSQL?
- Answer: PostgreSQL supports JSON and JSONB data types for storing JSON data. You can use functions and operators like
->,->>,#>>, and functions such asjsonb_set,jsonb_insert,jsonb_build_objectto query and manipulate JSON data.
- Answer: PostgreSQL supports JSON and JSONB data types for storing JSON data. You can use functions and operators like
-
What are the different types of joins in PostgreSQL?
- Answer: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN, and SELF JOIN.
-
Explain the concept of foreign data wrappers (FDW) in PostgreSQL.
- Answer: FDWs allow PostgreSQL to query data from other databases and data sources as if they were part of the local database. You can use
CREATE EXTENSIONto enable andCREATE SERVERandCREATE FOREIGN TABLEto configure.
- Answer: FDWs allow PostgreSQL to query data from other databases and data sources as if they were part of the local database. You can use
-
How does PostgreSQL handle replication?
- Answer: PostgreSQL supports streaming replication, logical replication, and synchronous replication. Streaming replication is typically used for high availability, while logical replication is used for data distribution and synchronization.
-
What is a materialized view and how do you refresh it?
- Answer: A materialized view stores the result of a query physically. You refresh it to update the data using:
REFRESH MATERIALIZED VIEW view_name;
- Answer: A materialized view stores the result of a query physically. You refresh it to update the data using:
-
How do you optimize query performance in PostgreSQL?
- Answer: Use indexes, analyze query plans using
EXPLAIN, tune database configuration parameters, use partitioning, optimize schema design, and use appropriate data types.
- Answer: Use indexes, analyze query plans using
-
What is the role of
pg_stat_statementsin PostgreSQL?- Answer:
pg_stat_statementsis an extension that tracks execution statistics of all SQL statements executed by a server. It helps in monitoring query performance and identifying slow queries.
- Answer:
-
Explain the
VACUUMcommand in PostgreSQL.- Answer: The
VACUUMcommand is used to reclaim storage occupied by dead tuples, which are outdated or deleted rows.VACUUM FULLrebuilds the entire table to reclaim the space, while a plainVACUUMonly cleans up.
- Answer: The
-
How do you set up connection pooling in PostgreSQL?
- Answer: Use a connection pooler like PgBouncer or Pgpool-II to manage database connections efficiently by reusing and limiting the number of connections.
- Write a query to find the nth highest salary from an employee table.
- Explain how to set up and configure a PostgreSQL cluster for high availability.
- Demonstrate how to use window functions in PostgreSQL.
- Show how to use triggers and stored procedures.
Preparing for these questions and understanding the underlying concepts will help you succeed in a PostgreSQL interview.
A local PostgreSQL instance is included through Docker Compose.
docker compose up -d
docker compose psUse the following local configuration for the sample application:
spring.datasource.url=jdbc:postgresql://localhost:5432/appdb
spring.datasource.username=appuser
spring.datasource.password=apppasswordFor real deployments, provide the URL and credentials through environment variables or a secret manager; do not commit them into source control. Stop the local database with docker compose down (add -v only when you intentionally want to remove its stored data).