Connect Streamlit to PostgreSQL
Introduction
This guide explains how to securely access a PostgreSQL database from Streamlit Cloud. It uses the psycopg2 library and Streamlit's secrets management.
Create a PostgreSQL database
If you already have a database that you want to use, feel free
to skip to the next step.
First, follow this tutorial to install PostgreSQL and create a database (note down the database name, username, and password!). Open the SQL Shell (psql) and enter the following two commands to create a table with some example values:
```sql
CREATE TABLE mytable (
    name            varchar(80),
    pet             varchar(80)
);
INSERT INTO mytable VALUES ('Mary', 'dog'), ('John', 'cat'), ('Robert', 'bird');
```
Add username and password to your local app secrets
Your local Streamlit app will read secrets from a file .streamlit/secrets.toml in your app's root directory. Create this file if it doesn't exist yet and add the name, user, and password of your database as shown below:
