How to Integrate Streamlit and CockroachDB Safely
Teams pair streamlit and cockroachdb to build live internal web apps over distributed SQL data. The web app runs Python scripts that query the database cluster and render charts. This setup lets analysts and support teams view live operational records without writing raw code. Still, connecting a front-end script directly to a production database creates real security risks. You must lock down how the app logs in, reads rows, and handles sensitive fields. How streamlit and cockroachdb share d
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 cockroachdb to build live internal web apps over distributed SQL data. The web app runs Python scripts that query the database cluster and render charts.
This setup lets analysts and support teams view live operational records without writing raw code. Still, connecting a front-end script directly to a production database creates real security risks. You must lock down how the app logs in, reads rows, and handles sensitive fields.
How streamlit and cockroachdb share data and permissions
Streamlit runs your Python script from top to bottom each time a user clicks a button. During a run, the app opens a database session using a standard Postgres driver. CockroachDB speaks the Postgres wire protocol, so standard Python database drivers work out of the box.
The app needs credentials to log in to the database cluster. Most teams store a username, password, and cluster address in a local secrets file. The database checks these credentials and assigns a SQL role to the session. That role controls which tables and schemas the Python script can read or change.
Once logged in, the app sends SQL queries over an encrypted TLS network path. CockroachDB runs the queries across its nodes and returns the matching rows to Python. The script then turns those rows into tables, charts, and forms in the user's browser. Using connection pooling in the app stops repeated script runs from opening too many sessions.
Common security gaps and how to scope database roles
The biggest mistake teams make is giving the web app an admin database role. If a shared service account has broad rights, one bad query can drop production tables. A flaw in user input handling could also let someone run arbitrary SQL commands.
Another major issue is the loss of user identity in database audit logs. Every user on the web app shares the exact same database login credentials. When a slow or risky query hits CockroachDB, the logs only show the shared app account. You cannot tell which team member actually triggered the query from their browser.
End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Raw query results can also expose private customer data on screen by mistake. A basic select query might pull emails, phone numbers, card numbers, or national IDs into memory. Data masking rewrites matched fields in results so sensitive strings do not appear in plain text.
To fix role bloat, create a dedicated database role for each internal web app. Grant that role read-only rights on the exact tables or views the app needs. Never let a reporting dashboard share a database role with an app that writes data.
Managing network paths and query safety in production
Production setups need strict network rules between the app host and the database cluster. Place both systems inside a private network and block public internet traffic to database ports. Set up network firewall rules so only the specific app server IP can reach the cluster.
Always require TLS certificate checks when the Python driver connects to the database nodes. This stops attackers on the network from reading data in transit or spoofing the cluster. You should also rotate the app's database passwords or client certificates on a regular schedule.
Finally, put guardrails on the queries that the Python script can send. Use parameterized queries for every user input to block SQL injection attacks. Set a statement timeout on the database role so runaway analytical queries do not lock up nodes.
Check database and API queries on the wire
Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.
Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.
Frequently asked questions
How should a Streamlit app log in to CockroachDB in production?
Use a dedicated, least-privilege SQL role and require full TLS certificate verification on every connection. Store the credentials in a secure secret store instead of plain source code files.
How do we stop Streamlit from opening too many CockroachDB connections?
Cache the database engine object in your Python script so sessions reuse a shared connection pool. This keeps the app from opening a fresh network session on every user click.
Can we restrict which sensitive columns show up in a Streamlit dashboard?
Yes, you can grant the app role access only to specific SQL views or columns. You can also use data masking to rewrite emails, phone numbers, and card numbers in results.
Related guides
In this post