WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PostgreSQL · Expert · question 71 of 100

How do you implement advanced security features in PostgreSQL, such as transparent data encryption (TDE), data masking, and row-level security?

📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

PostgreSQL provides various advanced security features to secure your data. Let’s discuss each feature in detail:

1. Transparent Data Encryption (TDE):

Transparent Data Encryption (TDE) is a security feature that encrypts the entire database on disk. It ensures that even if someone gains access to your database files, they cannot read the data without the key to decrypt it. PostgreSQL does not have built-in TDE support, but it can be implemented using third-party extensions like PostgreSQL Transparent Data Encryption (PostgreSQL TDE) or pgcrypto.

Here’s an example of how to use pgcrypto to encrypt a column:

import java.sql.*;
import org.postgresql.util.*;
...
// Establishing the connection
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "postgres", "password");
// Enable pgcrypto extension
Statement stmt = conn.createStatement();
stmt.executeUpdate("CREATE EXTENSION IF NOT EXISTS pgcrypto;");
// Encrypting a column using pgcrypto
PreparedStatement ps = conn.prepareStatement("UPDATE users SET password = crypt(?, gen_salt('bf')) WHERE id = ?");
ps.setString(1, "myPassword123");
ps.setInt(2, 1);
ps.executeUpdate();

2. Data Masking:

Data Masking is a security feature that obfuscates sensitive data to protect it from unauthorized access. PostgreSQL does not have built-in data masking support, but it can be implemented using third-party extensions like Postgres Masking and Safeguard.

Here’s an example of how to use Postgres Masking to mask a column:

import java.sql.*;
import de.zalando.pgobserver.msk.*;
...
// Establishing the connection
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "postgres", "password");
// Enable Postgres Masking extension
Statement stmt = conn.createStatement();
stmt.executeUpdate("CREATE EXTENSION IF NOT EXISTS masking;");
// Masking a column using Postgres Masking
PreparedStatement ps = conn.prepareStatement("SELECT mask_email(email) FROM users WHERE id = ?");
ps.setInt(1, 1);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
  System.out.println(rs.getString(1));
}

3. Row-Level Security:

Row-Level Security (RLS) is a security feature that restricts access to data at the row level. RLS ensures that only authorized users can view or modify selected rows in a table, based on predefined policies. RLS is built-in to PostgreSQL and can be implemented using policies defined in SQL statements.

Here’s an example of how to use RLS to restrict access to a table:

import java.sql.*;
...
// Establishing the connection
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "postgres", "password");
// Creating a table with RLS policy
Statement stmt = conn.createStatement();
stmt.executeUpdate("CREATE TABLE patients (id SERIAL PRIMARY KEY, name TEXT, ssn TEXT, doctor TEXT)");
stmt.executeUpdate("CREATE POLICY patients_policy ON patients TO doctors USING (doctor = session_user);");
// Inserting data into the table
PreparedStatement ps = conn.prepareStatement("INSERT INTO patients (name, ssn, doctor) VALUES (?, ?, ?)");
ps.setString(1, "John Smith");
ps.setString(2, "123-45-6789");
ps.setString(3, "Dr. Johnson");
ps.executeUpdate();
// Querying the table with RLS policy
ps = conn.prepareStatement("SELECT * FROM patients");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
  System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
}
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PostgreSQL interview — then scores it.
📞 Practice PostgreSQL — free 15 min
📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

All 100 PostgreSQL questions · All topics