Adding Postgres to an App

This documentation is for Dash Enterprise.
Dash Enterprise is the fastest way to write & deploy Dash apps and
Jupyter notebooks.
10% of the Fortune 500 uses Dash Enterprise to productionize AI and
data science apps. Find out if your company is using Dash Enterprise.

With Dash Enterprise, you have access to managed Postgres databases for your apps. Postgres is well-suited for many Dash apps. In particular, you can use Postgres to:

This page describes how to manage Postgres databases using the App Manager, but you can also use the Dash Enterprise CLI.

Adding Postgres to an App

Postgres instances are app-specific. Each app can have one Postgres instance. To use Postgres with an app, add it after you’ve created the app:

  1. Find and select the app in the App Manager.
  2. Select the Services tab.
  3. Select Add Postgres.

Add Postgres

  1. Select Add Postgres to confirm.

Referencing Postgres in Your Code

Once you add Postgres to an app, you can access Postgres in your app code in Dash Enterprise with:

connection_string = os.environ.get("DATABASE_URL")

For running Postgres locally on your own workstation, supply the local URL to be used outside of Dash Enterprise:

connection_string = os.environ.get("DATABASE_URL", <LOCAL_DATABASE_URL>)

Replacing <LOCAL_DATABASE_URL> with the URL of the database that you are running locally. For example, on Mac with the Postgres app, this might be ‘postgres://postgres:docker@127.0.0.1:5432’

If you are using the pg8000 driver, then this URL can be prefixed with pg8000+.

connection_string = "postgres+pg8000" + os.environ.get("DATABASE_URL", "postgres://postgres:docker@127.0.0.1:5432").lstrip("postgres")

The pg8000 driver is easy to install but isn’t production ready.

Pandas, SQLAlchemy, and Postgres Libraries

Not all versions of pandas, sqlalchemy, and Postgres libraries like psycopg2-binary and pg8000 are compatible. Here are some combinations we know to work well together:

pandas==1.4.1
psycopg2-binary==2.8.6
sqlalchemy==1.4.31
pandas==1.1.5
pg8000==1.16.5
SQLAlchemy==1.3.20

Using Postgres Inside Workspaces

Dash Enterprise Workspaces share the same Postgres instance as your deployed
app. This allows you to inspect your Postgres instance’s data from that app’s workspace.
Since these databases are shared, be careful when writing or deleting data from Postgres in a Workspace!

After adding Postgres to an app, you’ll need to restart Workspaces to have access to that database.

Running Postgres Locally

To test your app locally, you’ll need to install and start Postgres.
MacOS
Homebrew is a package manager for MacOS and makes it easy to install packages, including Postgres. This is the quickest way to get up and running developing with Postgres locally on a Mac.

  1. To install, in a terminal run:
    shell brew install postgresql
  2. Once installed, you can start your Postgres server with:
    shell brew services start postgres
  3. To connect to the default database:
    shell psql postgres

Once you’re finished using Postgres, you can stop the server with brew services stop postgres.

Other OS’s

You’ll find instructions for other OS and installers on the PostgreSQL site.

Deleting a Postgres Instance

To delete an app’s Postgres service:

  1. Find and select the app in the App Manager.

  2. Select the Services tab.

  3. Select the Postgres service. (The name ends in -postgres).

  4. Select Remove. You are prompted to enter the service-name to confirm.

Delete Postgres

Postgres Version and Settings

Dash Enterprise 5 uses Postgres version 14 from Docker Hub and its default settings. Right now it’s not possible to configure the settings.

Limits

Each Postgres instance has a maximum storage capacity of 25 GiB. Additionally, the number of services (Postgres or Redis) that you can have across Dash Enterprise is limited by the number of pods and volumes allowed per node by your Kubernetes cloud provider and the number of nodes in your cluster.

Troubleshooting

If you are an administrator with access to the Kubernetes cluster that Dash Enterprise is installed on, you can view logs for an app’s Postgres service by connecting to the cluster.

Managed database services are located in the dash-services namespace. To view logs for an app’s Postgres service, make sure your kubectl context is set to the right cluster, and then run the following command:

kubectl logs -f -n dash-services -l app=postgres-<app-name>

where <app-name> is the name of the app whose Postgres logs you want to view.