Skip to main content
PostgreSQL is the world’s most advanced open-source relational database. Connect your PostgreSQL database to Kasava to run custom SQL queries for goal metric tracking, giving you complete flexibility to track any KPI stored in your database.

What You Can Do

With the PostgreSQL integration, you can:
  • Track any database metric - Run custom SQL queries to pull any numeric value from your database
  • Use the Visual Query Builder - Build queries visually by selecting tables, columns, and aggregations without writing SQL
  • Write raw SQL queries - Full SQL flexibility for complex metrics, joins, and custom calculations
  • Filter by time ranges - Automatically filter data by last 24 hours, 7 days, 30 days, 90 days, or 365 days
  • Test queries before saving - Verify your query returns the expected value before linking to goals
  • Set refresh intervals - Choose hourly, daily, or weekly metric updates
  • Track multiple metrics - Connect to any number of tables and views in your database

Connecting PostgreSQL

1

Navigate to Settings

Click your profile menu in the top-right corner and select Settings, then go to Integrations
2

Find PostgreSQL

Scroll to the Analytics & Data section and locate the PostgreSQL integration card
3

Click Connect

Click the Connect button on the PostgreSQL card
4

Enter Connection Details

In the configuration dialog, enter:
  • Integration Name - A name to identify this connection (e.g., “Production Database”)
  • Host - Your database server hostname or IP address
  • Port - Database port (defaults to 5432)
  • Database Name - The name of your database
  • Username - Database user with read access
  • Password - Database password
  • Require SSL - Enable for secure connections (recommended)
5

Save

Click Save Integration to save your connection
PostgreSQL connection dialog with host, port, database, and credentials fields
For security, we recommend creating a read-only database user with access only to the tables you need for metrics. This minimizes risk while allowing Kasava to query your data.

Creating a Read-Only Database User

For production databases, create a dedicated read-only user:
-- Create a read-only user
CREATE USER kasava_reader WITH PASSWORD 'your_secure_password';

-- Grant connect access to your database
GRANT CONNECT ON DATABASE your_database TO kasava_reader;

-- Grant usage on the schema
GRANT USAGE ON SCHEMA public TO kasava_reader;

-- Grant SELECT on all current tables
GRANT SELECT ON ALL TABLES IN SCHEMA public TO kasava_reader;

-- Grant SELECT on future tables (optional)
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO kasava_reader;

Linking Metrics to Goals

Once connected, you can link PostgreSQL metrics to your product goals for automatic progress tracking.
1

Open a Goal

Navigate to your product’s Analysis section, select the Goals tab, and click on a goal
2

Add a Metric

In the goal detail dialog, click Add Metric in the metrics section
3

Select PostgreSQL

Choose your PostgreSQL integration from the metric source picker
4

Choose Query Mode

Select either Visual Builder or Raw SQL mode (see below for details)
5

Configure and Test

Configure your metric settings and click Test Query to verify it works
6

Save

Click Link Metric to add the metric to your goal
Link Metric dialog showing PostgreSQL query configuration with Visual Builder selected

Query Modes

PostgreSQL supports two query modes for maximum flexibility:

Visual Query Builder

Build queries visually without writing SQL. Perfect for common aggregations.
SettingDescription
TableSelect from available tables and views in your database
AggregationCount (rows), Sum, Average, Maximum, Minimum, Count Distinct
ColumnFor non-count aggregations, select the numeric column to aggregate
Time ColumnOptional - select a timestamp column to filter by time
Time RangeLast 24 hours, 7 days, 30 days, 90 days, 365 days, or All time
Example configurations:
  • Count all users: Table = users, Aggregation = Count (rows)
  • Sum of revenue: Table = orders, Aggregation = Sum, Column = amount
  • Average order value: Table = orders, Aggregation = Average, Column = amount
  • New users this month: Table = users, Aggregation = Count (rows), Time Column = created_at, Time Range = Last 30 days

Raw SQL

Write custom SQL for complex metrics. Your query must return exactly one row with one numeric column.
-- Example: Count active users in the last 30 days
SELECT COUNT(*)
FROM users
WHERE last_login > NOW() - INTERVAL '30 days'

-- Example: Calculate MRR from subscriptions
SELECT SUM(monthly_amount)
FROM subscriptions
WHERE status = 'active'

-- Example: Average response time
SELECT AVG(response_time_ms)
FROM api_requests
WHERE created_at > NOW() - INTERVAL '7 days'

-- Example: Conversion rate calculation
SELECT
  (COUNT(*) FILTER (WHERE converted = true)::float /
   NULLIF(COUNT(*), 0) * 100)::numeric(5,2)
FROM signups
WHERE created_at > NOW() - INTERVAL '30 days'
Your query must return exactly one row with one numeric column. Queries returning multiple rows or non-numeric values will fail.

Aggregation Types

When using the Visual Builder, you can choose from these aggregation functions:
AggregationDescriptionColumn Required
Count (rows)Counts the number of rowsNo
SumSums all values in a numeric columnYes (numeric)
AverageCalculates the mean of a numeric columnYes (numeric)
MaximumFinds the highest valueYes (numeric)
MinimumFinds the lowest valueYes (numeric)
Count DistinctCounts unique values in a columnYes (any type)

Time Filtering

Add time-based filtering to track metrics over specific periods:
Time RangeDescription
Last 24 hoursData from the past day
Last 7 daysData from the past week
Last 30 daysData from the past month
Last 90 daysData from the past quarter
Last 365 daysData from the past year
All timeNo time filter applied
When using the Visual Builder:
  1. Select a Time Column (must be a timestamp, date, or datetime column)
  2. Choose your desired Time Range
The Visual Builder automatically detects date/time columns in your selected table.

Refresh Intervals

Choose how often Kasava fetches new data from your database:
IntervalDescription
HourlyUpdates every hour - best for real-time dashboards
DailyUpdates once per day - best for most goals
WeeklyUpdates once per week - best for long-term trends
For most goals, daily refresh is recommended. Use hourly only for time-sensitive metrics where you need frequent updates.

Testing Your Query

After configuring a metric query, always click Test Query before saving. This will:
  1. Connect to your database using the stored credentials
  2. Execute your query against the database
  3. Display the current metric value
  4. Confirm the configuration returns valid results
Test query result showing successful value retrieval with current metric value A successful test shows a green success message with the current value. If the test fails, you’ll see an error message explaining what went wrong.

Managing Integrations

View Integration Status

Navigate to Settings > Integrations to see all connected PostgreSQL integrations and their status.

Create Integration Metrics

You can create reusable metrics at the integration level that can be linked to multiple goals:
1

Go to Integration Settings

Navigate to Settings > Integrations and click Manage on your PostgreSQL integration
2

Create Metric

Click Add Metric to create a new integration-level metric
3

Configure

Set up the query configuration using Visual Builder or Raw SQL
4

Link to Goals

The metric can now be linked to any goal in your organization

Disconnect Integration

1

Go to Settings

Navigate to Settings > Integrations
2

Find PostgreSQL

Locate the PostgreSQL integration card (it will have a green “Connected” indicator)
3

Click Manage

Click the Manage button on the card
4

Disconnect

Click Disconnect and confirm to remove the integration
Disconnecting will stop all metric syncing for goals linked to this integration. Historical data will be preserved, but no new data will be fetched.

Use Cases

Product Metrics

Track user counts, feature adoption, and engagement:
-- Daily Active Users
SELECT COUNT(DISTINCT user_id)
FROM events
WHERE created_at > NOW() - INTERVAL '24 hours'

-- Feature adoption rate
SELECT COUNT(DISTINCT user_id)
FROM feature_usage
WHERE feature_name = 'new_dashboard'
  AND created_at > NOW() - INTERVAL '30 days'

Business Metrics

Monitor revenue, subscriptions, and customer data:
-- Monthly Recurring Revenue
SELECT SUM(monthly_amount)
FROM subscriptions
WHERE status = 'active'

-- Customer count by plan
SELECT COUNT(*)
FROM customers
WHERE plan_type = 'enterprise'
  AND status = 'active'

Technical Metrics

Track API performance, error rates, and system health:
-- API error rate (percentage)
SELECT
  (COUNT(*) FILTER (WHERE status_code >= 400)::float /
   NULLIF(COUNT(*), 0) * 100)::numeric(5,2)
FROM api_logs
WHERE created_at > NOW() - INTERVAL '24 hours'

-- Average response time
SELECT AVG(duration_ms)
FROM api_logs
WHERE created_at > NOW() - INTERVAL '1 hour'

Troubleshooting

Verify your database host is accessible from the internet. If using a private network, you may need to whitelist Kasava’s IP addresses or set up a secure tunnel.
Enable the Require SSL checkbox in the connection settings. Most cloud database providers require SSL connections.
The database user doesn’t have SELECT permissions on the requested table. Grant access with:
GRANT SELECT ON table_name TO your_user;
Your query must return exactly one row. Wrap aggregations in your query or ensure your WHERE clause matches at least one record. Use COALESCE to handle empty results:
SELECT COALESCE(COUNT(*), 0) FROM users WHERE ...
The query must return a numeric value. Cast text or other types to numeric:
SELECT COUNT(*)::numeric FROM users
Check if your database allows connections from external IPs. Cloud databases often require you to add Kasava’s IP addresses to an allowlist.
The database user may not have access to the information_schema or lacks SELECT permissions on certain tables. Verify permissions and try refreshing the table list.

Security Best Practices

Create a dedicated read-only user - Never use admin credentials. Create a user with only SELECT permissions on the specific tables you need.
Enable SSL connections - Always use SSL when connecting over the internet to encrypt data in transit.
Limit table access - Grant access only to tables needed for metrics, not your entire database.
Use connection pooling - If you have many metrics, consider using a connection pooler like PgBouncer to manage database connections efficiently.