Connect Streamlit to Google Sheets and Use It as a Simple Database
Google Sheets works well as a lightweight database for a Streamlit app. With the st-gsheets-connection package, a public sheet needs only its URL; a private one needs a Google service account whose credentials live in Streamlit’s secrets file. Both routes are shown, with a working example.
Public sheet
A publicly shared sheet needs no credentials and gives read-only access: the package, the sheet URL and three lines of code. Install the package from the terminal, save the example as streamlit_app.py and start it with streamlit run.
pip install st-gsheets-connection
streamlit run streamlit_app.py
The code opens a connection, reads the sheet at the URL and shows it as a DataFrame. The video uses st.experimental_connection, which current Streamlit versions no longer have; the code below uses its replacement, st.connection, with the same arguments.
import streamlit as st
from streamlit_gsheets import GSheetsConnection
url = "https://docs.google.com/spreadsheets/d/1JDy9md2VZPz4JbYtRPJLs81_3jUK47nx6GYQjgU8qNY/edit?usp=sharing"
conn = st.connection("gsheets", type=GSheetsConnection)
data = conn.read(spreadsheet=url, usecols=[0, 1])
st.dataframe(data)
usecols picks the columns by index, so [0, 1] reads columns A and B. To read a tab other than the first one of a public sheet, pass its worksheet ID: the number after gid= in the URL.

import streamlit as st
from streamlit_gsheets import GSheetsConnection
url = "https://docs.google.com/spreadsheets/d/1JDy9md2VZPz4JbYtRPJLs81_3jUK47nx6GYQjgU8qNY/edit?usp=sharing"
conn = st.connection("gsheets", type=GSheetsConnection)
data = conn.read(spreadsheet=url, usecols=list(range(2)), worksheet="1453155026")
st.dataframe(data)
Each tab then works like a table in a relational database. If your source is an Excel file rather than a Google Sheet, here is how I export Excel data to Google Sheets first.
Private sheet
A private sheet needs a Google Cloud project with the Drive and Sheets APIs enabled, a service account, and that account’s key in Streamlit’s secrets file. The sheet is then shared with the service account like with any other user, which also allows writing.
-
Create a project and enable the APIs. In the Google Developers Console, create a new project and enable the Google Drive API and the Google Sheets API.

-
Create a service account. Under Credentials, create a service account, give it a name and the role Editor so it can read and modify data.

-
Download the key. Create a JSON key for the service account and download it. Treat this file like a password and keep it out of Git.
-
Fill the secrets file. Create a folder named
.streamlitin the app’s root folder with asecrets.tomlinside, and copy the values from the JSON key into it together with the spreadsheet link. -
Share the sheet. Share the spreadsheet with the
client_emailfrom the JSON key, with Editor access.
With the key in the secrets file, the connection needs no URL in the code. This app reads the Product tab and runs an SQL query that lists products below their reorder level:
import streamlit as st
from streamlit_gsheets import GSheetsConnection
conn = st.connection("gsheets", type=GSheetsConnection)
data = conn.read(worksheet="Product")
st.dataframe(data)
st.subheader("Inventory Health Check 📦")
sql = '''
SELECT
"Product ID",
"Product Name",
"Supplier",
"Current Inventory Level",
"Reorder Level"
FROM
Product
WHERE
"Current Inventory Level" < "Reorder Level"
ORDER BY
"Reorder Level" DESC;
'''
df_inventory_health = conn.query(sql=sql)
st.dataframe(df_inventory_health)
If you skip the sharing step, the app stops with this error:
APIError: {'code': 403, 'message': 'The caller does not have permission', 'status': 'PERMISSION_DENIED'}
Share the sheet with the service account email, refresh the app, and the Product table and the inventory query load.
If you only need to append form entries and want to skip the Google Cloud console, a no-code tool behind a webhook can write the rows for you.
Create, query, update, clear
With the private connection in place, the sheet behaves like a small database: the connection can create a worksheet from a DataFrame, query it with SQL, overwrite it with new data and clear it. The demo app below does each of these from a button, using a sample DataFrame of orders.
import streamlit as st
import pandas as pd
from streamlit_gsheets import GSheetsConnection
st.title("Google Sheets as a DataBase")
# Function to create a sample Orders dataframe
def create_orders_dataframe():
return pd.DataFrame({
'OrderID': [101, 102, 103, 104, 105],
'CustomerName': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'ProductList': ['ProductA, ProductB', 'ProductC', 'ProductA, ProductC', 'ProductB, ProductD', 'ProductD'],
'TotalPrice': [200, 150, 250, 300, 100],
'OrderDate': ['2023-08-18', '2023-08-19', '2023-08-19', '2023-08-20', '2023-08-20']
})
# Create the Orders dataframe
orders = create_orders_dataframe()
# Update the TotalPrice column in the orders dataframe to create updated_orders
updated_orders = orders.copy()
updated_orders['TotalPrice'] = updated_orders['TotalPrice'] * 100
with st.expander("Data ⤵"):
st.write("Orders")
st.dataframe(orders)
st.write("Updated Orders")
st.dataframe(updated_orders)
st.divider()
st.write("CRUD Operations:")
# Establishing a Google Sheets connection
conn = st.connection("gsheets", type=GSheetsConnection)
# Taking actions based on user input
if st.button("New Worksheet"):
conn.create(worksheet="Orders", data=orders)
st.success("Worksheet Created 🎉")
if st.button("Calculate Total Orders Sum"):
sql = 'SELECT SUM("TotalPrice") as "TotalOrdersPrice" FROM Orders;'
total_orders = conn.query(sql=sql) # default ttl=3600 seconds / 60 min
st.dataframe(total_orders)
if st.button("Update Worksheet"):
conn.update(worksheet="Orders", data=updated_orders)
st.success("Worksheet Updated 🤓")
if st.button("Clear Worksheet"):
conn.clear(worksheet="Orders")
st.success("Worksheet Cleared 🧹")
New Worksheet adds an Orders tab with the sample data. The SQL query sums the TotalPrice column, and Update Worksheet writes the same orders with every price multiplied by 100. To collect rows from users instead of a fixed DataFrame, see my Google Sheets data entry form in Streamlit.

Caching and limits
The connection caches results for 60 minutes by default, so a query can return old numbers after the sheet changed. After Update Worksheet multiplied every price by 100, the sum below still shows 1,000 because the query result came from the cache.

For testing, I set the TTL to 5 seconds so changes show up almost at once:
total_orders = conn.query(sql=sql, ttl=5) # cache the result for 5 seconds instead of the default 3600
Google Sheets does not replace a real database for larger projects. Every uncached read is a call to the Google API, which has request quotas, and an update writes the whole worksheet, so two users saving at the same moment can overwrite each other. For a small internal app or a prototype with a well-structured sheet, it works fine.
