Ritesh
07/09/2025, 9:07 AMMarvin
07/09/2025, 9:07 AMMarvin
07/09/2025, 9:08 AMPREFECT_API_DATABASE_CONNECTION_URL
environment variable. You can include the schema in the connection URL:
bash
export PREFECT_API_DATABASE_CONNECTION_URL="<postgresql+asyncpg://username:password@host:port/database?options=-csearch_path%3Dyourschema>"
Or you can set it using the Prefect CLI:
bash
prefect config set PREFECT_API_DATABASE_CONNECTION_URL="<postgresql+asyncpg://username:password@host:port/database?options=-csearch_path%3Dyourschema>"
2. If you're using Docker Compose, you can specify this in your `docker-compose.yml`:
yaml
services:
prefect:
image: prefecthq/prefect:latest
environment:
- PREFECT_API_DATABASE_CONNECTION_URL=<postgresql+asyncpg://username:password@db:5432/database?options=-csearch_path%3Dyourschema>
# ... other configuration
Important notes:
- Make sure the PostgreSQL user has the necessary permissions on the specified schema
- The database must have the pg_trgm
extension installed
- The schema must exist before starting the Prefect server
- The URL must use the asyncpg
driver as shown in the examples
If you need to create the schema first, you can do so using SQL:
sql
CREATE SCHEMA IF NOT EXISTS yourschema;
GRANT ALL ON SCHEMA yourschema TO your_prefect_user;
Keep in mind that Prefect will create its own tables in the specified schema when it first starts up. The schema should be empty or at least not contain tables that conflict with Prefect's required tables.
For production environments, it's recommended to:
- Set up proper role-based access control in PostgreSQL
- Consider implementing regular maintenance tasks for database cleanup
- Monitor the database size and performance
- Ensure proper backup procedures are in placeRitesh
07/10/2025, 5:19 AMMarvin
07/10/2025, 5:19 AMMarvin
07/10/2025, 5:21 AMsearch_path
parameter directly in the connection URL. Here's the proper syntax:
bash
export PREFECT_API_DATABASE_CONNECTION_URL="<postgresql+asyncpg://username:password@host:port/database?search_path=yourschema>"
Or if using Docker Compose:
yaml
services:
prefect:
image: prefecthq/prefect:latest
environment:
- PREFECT_API_DATABASE_CONNECTION_URL=<postgresql+asyncpg://username:password@db:5432/database?search_path=yourschema>
# ... other configuration
The key differences from the previous answer are:
1. Remove the options=-c
part
2. Use search_path
directly as a URL parameter
Make sure that:
1. The schema exists in your database
2. Your database user has the proper permissions on that schema
3. The schema name is correct and case-sensitive
If you're still experiencing issues, could you share the exact connection string you're using (with sensitive information redacted)?Ritesh
07/10/2025, 5:22 AMMarvin
07/10/2025, 5:22 AMMarvin
07/10/2025, 5:23 AMsql
-- First, create the schema if it doesn't exist
CREATE SCHEMA IF NOT EXISTS yourschema;
-- Grant necessary permissions
GRANT ALL ON SCHEMA yourschema TO your_prefect_user;
-- Set it as the default schema for your user
ALTER USER your_prefect_user SET search_path TO yourschema;
Then use the regular connection URL:
bash
export PREFECT_API_DATABASE_CONNECTION_URL="<postgresql+asyncpg://username:password@host:port/database>"
2. Or, if you need to keep the default schema for the user but want Prefect to use a specific schema, you can create the schema and then move the Prefect tables into it after initial setup:
sql
-- After Prefect has created its tables in the public schema
ALTER TABLE table_name SET SCHEMA yourschema;
-- Repeat for all Prefect tables
The first approach (setting the default schema for the user) is generally cleaner and recommended.
If you're using Docker Compose, the connection string would look like:
yaml
services:
prefect:
image: prefecthq/prefect:latest
environment:
- PREFECT_API_DATABASE_CONNECTION_URL=<postgresql+asyncpg://username:password@db:5432/database>
# ... other configuration
Remember to:
1. Create the schema first
2. Grant appropriate permissions
3. Set it as the default schema for your user
4. Ensure the user has all necessary permissions within that schema
Would you like me to provide the complete list of SQL commands needed to set this up properly?