To self-host a Prefect server instance on Kubernetes, check out the prefect-server Helm chart.

After installing Prefect, you have a Python SDK client that can communicate with either Prefect Cloud or a self-hosted Prefect server, backed by a database and a UI.

Prefect Cloud and self-hosted Prefect server share a common set of capabilities. Prefect Cloud provides the additional features required by organizations such as RBAC, Audit logs, and SSO. See the Prefect Cloud overview for more information.

Prefect server installation notes

Your self-hosted server must meet the following requirements and configuration settings.

SQLite

SQLite is not packaged with the Prefect installation. But most systems already have SQLite installed, and it is typically bundled with Python.

If you self-host a Prefect server instance with a SQLite database, certain Linux versions of SQLite can be problematic. Compatible versions include Ubuntu 22.04 LTS and Ubuntu 20.04 LTS.

To confirm SQLite is installed, run:

sqlite3 --version

Use a self-signed SSL certificate

When using a self-signed SSL certificate, you need to configure your environment to trust the certificate. Add the certificate to your system bundle and point your tools to use that bundle by configuring the SSL_CERT_FILE environment variable.

If the certificate is not part of your system bundle, set the PREFECT_API_TLS_INSECURE_SKIP_VERIFY to True to disable certificate verification altogether.

Disabling certificate validation is insecure and only suggested as an option for testing.

Proxies

Prefect supports communicating with proxies through environment variables. Whether you use a Prefect Cloud account or self-host a Prefect server instance, set HTTPS_PROXY and SSL_CERT_FILE in your environment. Then the underlying network libraries will route Prefect’s requests appropriately.

Alternatively, the Prefect library connects to the API through any proxies you have listed in the HTTP_PROXY or ALL_PROXY environment variables. You may also use the NO_PROXY environment variable to specify which hosts should not pass through the proxy.

For more information about these environment variables, see the cURL documentation.

Self-host a Prefect server

  1. Spin up a self-hosted Prefect server instance UI with the prefect server start CLI command in the terminal:
prefect server start
  1. Open the URL for the Prefect server UI (http://127.0.0.1:4200 by default) in a browser.

  1. Shut down the Prefect server with ctrl + c in the terminal.

Configure self-hosted Prefect server

Go to your terminal session and run this command to set the API URL to point to a sefl-hosted Prefect server instance:

prefect config set PREFECT_API_URL="http://127.0.0.1:4200/api"

You must set the API server address, PREFECT_API_URL, to use Prefect within a container, such as a Docker container.

You can save the API server address in a Prefect profile. Whenever that profile is active, the API endpoint is at that address.

See Profiles and configuration for more information on profiles and configurable Prefect settings.

The Prefect database

The Prefect database persists data to track the state of your flow runs and related Prefect concepts, including:

  • Flow run and task run state
  • Run history
  • Logs
  • Deployments
  • Flow and task run concurrency limits
  • Storage blocks for flow and task results
  • Variables
  • Artifacts
  • Work pool status

Prefect supports the following databases:

  • SQLite (default in Prefect): Recommended for lightweight, single-server deployments. SQLite requires essentially no setup.
  • PostgreSQL: Best for connecting to external databases, but requires additional setup (such as Docker). Prefect uses the pg_trgm extension, so it must be installed and enabled.

Using the database

A local SQLite database is the default database and is configured upon Prefect installation. The database is located at ~/.prefect/prefect.db by default.

To reset your database, run the CLI command:

prefect server database reset -y

This command clears all data and reapplies the schema.

Database settings

Prefect provides several settings for configuring the database. The default settings are:

PREFECT_API_DATABASE_CONNECTION_URL='sqlite+aiosqlite:///${PREFECT_HOME}/prefect.db'
PREFECT_API_DATABASE_ECHO='False'
PREFECT_API_DATABASE_MIGRATE_ON_START='True'
PREFECT_API_DATABASE_PASSWORD='None'

Save a setting to your active Prefect profile with prefect config set.

Configure a PostgreSQL database

Connect Prefect to a PostgreSQL database by setting the following environment variable:

prefect config set PREFECT_API_DATABASE_CONNECTION_URL="postgresql+asyncpg://postgres:yourTopSecretPassword@localhost:5432/prefect"

The above environment variable assumes:

  • You have a username called postgres
  • Your password is set to yourTopSecretPassword
  • Your database runs on the same host as the Prefect server instance, localhost
  • You use the default PostgreSQL port 5432
  • Your PostgreSQL instance has a database called prefect

Quickstart: configure a PostgreSQL database with Docker

Start a PostgreSQL instance to use as your Prefect database with the following command (which starts a Docker container running PostgreSQL):

docker run -d --name prefect-postgres -v prefectdb:/var/lib/postgresql/data -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=yourTopSecretPassword -e POSTGRES_DB=prefect postgres:latest

The above command:

  • Pulls the latest version of the official postgres Docker image, which is compatible with Prefect.
  • Starts a container with the name prefect-postgres.
  • Creates a database prefect with a user postgres and yourTopSecretPassword password.
  • Mounts the PostgreSQL data to a Docker volume called prefectdb to provide persistence if you ever have to restart or rebuild that container.

Run the command below to set your current Prefect Profile to the PostgreSQL database instance running in your Docker container.

prefect config set PREFECT_API_DATABASE_CONNECTION_URL="postgresql+asyncpg://postgres:yourTopSecretPassword@localhost:5432/prefect"

Confirm your PostgreSQL database configuration

Inspect your Prefect profile to confirm that the environment variable has been properly set:

prefect config view --show-sources
You should see output similar to the following:

PREFECT_PROFILE='my_profile'
PREFECT_API_DATABASE_CONNECTION_URL='********' (from profile)
PREFECT_API_URL='http://127.0.0.1:4200/api' (from profile)

Start the Prefect server to use your PostgreSQL database instance:

prefect server start

In-memory database

To use an in-memory SQLite database, set the following environment variable:

prefect config set PREFECT_API_DATABASE_CONNECTION_URL="sqlite+aiosqlite:///file::memory:?cache=shared&uri=true&check_same_thread=false"

Use SQLite database for testing only

SQLite does not support multiprocessing. For high orchestration volume, use PostgreSQL.

Migrations

Prefect uses Alembic to manage database migrations. Alembic is a database migration tool to use with the SQLAlchemy Database Toolkit for Python. Alembic provides a framework for generating and applying schema changes to a database.

Apply migrations to your database with the following commands:

To upgrade:

prefect server database upgrade -y

To downgrade:

prefect server database downgrade -y

Use the -r flag to specify a specific migration version to upgrade or downgrade to. For example, to downgrade to the previous migration version, run:

prefect server database downgrade -y -r -1

or to downgrade to a specific revision:

prefect server database downgrade -y -r d20618ce678e

To downgrade all migrations, use the base revision.

See the contributing docs to learn how to create a database migration.