How to Integrate Drizzle and Postgres Safely

Published
Length
3 min read

Luis Felipe Dias

All posts

Integrating drizzle and postgres lets your TypeScript app run type-safe SQL queries against your database. This setup maps your code schema to database tables for fast reads, writes, and migrations. In production, this pair handles both daily app traffic and schema changes. Many teams use one shared database user for both tasks. That habit creates high risk if a key leaks or a bad migration runs. You can fix this by splitting roles and locking down database rights. How data and permissions fl

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

Integrating drizzle and postgres lets your TypeScript app run type-safe SQL queries against your database. This setup maps your code schema to database tables for fast reads, writes, and migrations.

In production, this pair handles both daily app traffic and schema changes. Many teams use one shared database user for both tasks. That habit creates high risk if a key leaks or a bad migration runs. You can fix this by splitting roles and locking down database rights.

How data and permissions flow between drizzle and postgres

Drizzle acts as a thin TypeScript layer on top of a standard database driver. Your app builds a query in code, and the driver sends SQL over the network. The database checks the login details and runs the statement under that database role.

Every action depends on the privileges of the active database role. By default, the special PUBLIC role gives every user certain base rights on a database. Those default rights include connecting, making temporary tables, and running functions. The database owner can revoke these default PUBLIC grants to lock down the system.

Your app does not need full access to every column in every table. In Postgres, you can grant the SELECT privilege on specific columns of a table. This privilege also lets the user run data exports on those allowed columns. If you restrict column rights, a query for sensitive fields will fail at the database level.

Common failure modes and how to scope database roles

The biggest mistake is running app queries and schema migrations under one owner account. If the web app holds schema change rights, one bug can drop a production table. A shared service account also hides who ran a manual query during an incident.

Start by creating separate roles for migrations, app traffic, and human debugging. Use group roles to manage permissions when you have many users on your team. You grant table rights to the group role, then add individual users to that group.

End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Next, check who can create new tables in your main schema. In PostgreSQL 15 and later, regular users cannot create objects in the public schema by default. Older databases, or ones upgraded from older versions, still allow this unless an admin revokes it. Always revoke create rights from the PUBLIC role on the public schema in upgraded systems.

Developers often run local tools to inspect data or test schema updates. Giving every engineer a shared admin password leads to unlogged queries and compliance gaps. Instead, give human users read-only group roles and mask sensitive fields like emails or phone numbers. Masking rewrites matched fields in query results so raw card numbers or national IDs stay protected.

Securing schema migrations and developer workflows

Schema migrations need high privileges to alter tables and add indexes. Keep the migration role locked inside your deployment pipeline. Your daily web app should use a separate role that only reads and writes data.

When engineers need to check production data with local Drizzle tools, avoid static database keys. Tie each session to a real person so your audit logs show who ran which query. Keep network paths tight and require encryption for all traffic between your app and the database.

Check every Postgres query on the wire

Database roles and network rules decide who can open a session. They do not read what each SQL statement does after login. With hoop.dev, you run an open-source proxy, a sidecar for your Postgres. It runs beside your database and reads the Postgres wire protocol. Your apps and agents keep their login details and change only the host and port in the connection string their tool uses.

Before a query runs, hoop.dev guardrails check the statement by its effect, table, or pattern. They refuse bad deletes or drops with a real Postgres error. On the way back, hoop.dev data masking rewrites sensitive values in returned rows based on your rules. Every query and verdict goes to the audit trail. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for HIPAA, GDPR, and PCI DSS. See the code on GitHub.

Frequently asked questions

Should my app use the same Postgres role for queries and Drizzle migrations?

No, you should always split these into two separate roles. Give the migration role schema change rights, and limit the app role to reading and writing rows.

How do I stop an app role from reading sensitive columns in Postgres?

You can grant the SELECT privilege on specific columns instead of the whole table. Keep sensitive columns off that list so the database blocks unauthorized reads.

Why can regular database users create tables in my public schema?

Databases created before PostgreSQL 15 allowed all users to create objects in the public schema. An admin must revoke the create privilege from the PUBLIC role to stop this.

The query runs. The emails stay hidden.