How to Integrate Streamlit and Postgres Safely

Published
Length
3 min read

Luis Felipe Dias

All posts

Teams pair streamlit and postgres to turn live SQL queries into web dashboards. The Python app sends queries to the database and renders the returned rows. Data teams build internal tools with this setup to review metrics and check records. Still, fast prototypes often move into production with weak database rules. A safe setup requires strict role limits, clear network rules, and careful query tracking. How streamlit and postgres handle login and data flow When a user opens the web app, the

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.

Teams pair streamlit and postgres to turn live SQL queries into web dashboards. The Python app sends queries to the database and renders the returned rows.

Data teams build internal tools with this setup to review metrics and check records. Still, fast prototypes often move into production with weak database rules. A safe setup requires strict role limits, clear network rules, and careful query tracking.

How streamlit and postgres handle login and data flow

When a user opens the web app, the Python script runs from top to bottom. The app reads stored database keys and opens a network link to the database server. Every click or filter change on the page can trigger a new SQL query.

The database checks the role tied to those keys before running any command. It looks at schema rules and table rules to decide if the query can run. If the role has permission, the server reads the tables and sends rows back.

In PostgreSQL, the SELECT privilege lets a role read from a table or view. It also allows the COPY TO command to export data. You can grant SELECT on a whole table or only on specific columns. Column limits help keep emails, phone numbers, card numbers, and national IDs out of dashboards.

Database admins can also set up group roles when many apps or users need access. You grant table permissions to the group role first. Then you add specific app roles as members of that group role.

Common failure modes with shared service accounts

Many teams use one shared database login for every internal dashboard. If that single password leaks, an attacker gains broad reach across your data tables. Shared logins also ruin audit logs because every query comes from the same account.

Overbroad database roles make this problem much worse. A dashboard that only charts sales numbers should never have permission to drop tables. Yet developers often reuse an admin role or a table owner role to save time.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Default permissions in the database can also create hidden risks for your team. The special PUBLIC role grants rights to every role on the system at once. By default, PUBLIC gets the right to link to databases and create temp tables. It also gets the right to run functions and procedures unless the owner revokes it.

In PostgreSQL 15 and later, users cannot create objects in the public schema by default. Older databases, or upgraded ones, may still allow this unless an admin revokes CREATE from PUBLIC. If your app role can create objects or run functions, bad inputs can cause damage.

How to scope database roles and network paths

Start by creating a dedicated, read-only database role for each web app you deploy. Revoke the default PUBLIC rights that your dashboard does not need to run. Grant the app role SELECT only on the exact tables and columns it displays.

If several dashboards need the same read access, put those permissions in a group role. Grant your individual app roles membership in that group role to keep rules clean. Never let a web app log in as a superuser or schema owner.

Next, lock down the network path between the app host and the database server. Place both systems in private network subnets so the database never faces the public internet. Set firewall rules so the database port only accepts traffic from the app host.

Finally, make sure you know which person triggered each query inside the dashboard. Standard app pools hide the end user because every query uses the same database role. Teams need a way to tie app sessions back to real user identities and mask sensitive columns like emails or card numbers in query results.

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 a dashboard use a database owner role?

No, an app should never use an owner or admin role. Create a separate role with SELECT rights on only the needed tables and columns.

How can I stop an app role from running every database function?

By default, the PUBLIC role can run functions and procedures. The object owner must revoke those default rights from PUBLIC and grant them only to trusted roles.

How do I keep emails and card numbers out of dashboard queries?

You can grant SELECT on specific columns in PostgreSQL so the role only reads safe fields. You can also use data masking to rewrite emails and card numbers in query results.

The query runs. The emails stay hidden.