Docs >
Database Monitoring >
Setting up Postgres >
Setting Up Database Monitoring for Supabase
Database Monitoring provides deep visibility into your Supabase databases by exposing query metrics, query samples, explain plans, database states, failovers, and events.
The Agent collects telemetry directly from the database by logging in as a read-only user. Do the following setup to enable Database Monitoring with your Supabase database:
The default Agent configuration for Database Monitoring is conservative, but you can adjust settings such as the collection interval and query sampling rate to better suit your needs. For most workloads, the Agent represents less than one percent of query execution time on the database and less than one percent of CPU.
Database Monitoring runs as an integration on top of the base Agent (see benchmarks).
Proxies, load balancers, and connection poolers
The Datadog Agent must connect directly to the host being monitored. For self-hosted databases, 127.0.0.1 or the socket is preferred. The Agent should not connect to the database through a proxy, load balancer, or connection pooler such as Supabase’s Dedicated Pooler (pgbouncer) or Session Pooler (Supavisor). If the Agent connects to different hosts while it is running (as in the case of failover, load balancing, and so on), the Agent calculates the difference in statistics between two hosts, producing inaccurate metrics.
Data security considerations
See Sensitive information for information about what data the Agent collects from your databases and how to ensure it is secure.
Grant the Agent access
The Datadog Agent requires read-only access to the database server in order to collect statistics and queries.
The following SQL commands should be executed on the primary database server (the writer) in the cluster if Supabase is replicated. Choose a Supabase database on the database server for the Agent to connect to. The Agent can collect telemetry from all databases on the database server regardless of which one it connects to, so a good option is to use the default postgres database. Choose a different database only if you need the Agent to run custom queries against data unique to that database.
Connect to the chosen database as a superuser (or another user with sufficient permissions). For example, if your chosen database is postgres, connect as the postgres user.
Navigate to the SQL Editor tab inside Supabase and run:
CREATEUSERdatadogWITHpassword'<PASSWORD>';
Give the datadog user permission to relevant tables:
In addition, ensure that the pg_stat_statements extension is enabled for your project in Supabase.```
For data collection or custom metrics that require querying additional tables, you may need to grant the SELECT permission on those tables to the datadog user. Example: grant SELECT on <TABLE_NAME> to datadog;. See PostgreSQL custom metric collection for more information.
Create the following function in every database to enable the Agent to collect explain plans.
Store your password using secret management software such as Vault. You can then reference this password as ENC[<SECRET_NAME>] in your Agent configuration files: for example, ENC[datadog_user_database_password]. See Secrets Management for more information.
The examples on this page use datadog_user_database_password to refer to the name of the secret where your password is stored. It is possible to reference your password in plain text, but this is not recommended.
Verify
To verify that the permissions are correct, connect to the database as the datadog user and run the following commands. For example, if your database is postgres, connect as the datadog user using psql and run:
psql -h {SUPABASE_HOST} -U datadog postgres -A \
-c "select * from pg_stat_database limit 1;"\
&&echo -e "\e[0;32mPostgres connection - OK\e[0m"\
||echo -e "\e[0;31mCannot connect to Postgres\e[0m"psql -h {SUPABASE_HOST} -U datadog postgres -A \
-c "select * from pg_stat_activity limit 1;"\
&&echo -e "\e[0;32mPostgres pg_stat_activity read OK\e[0m"\
||echo -e "\e[0;31mCannot read from pg_stat_activity\e[0m"psql -h {SUPABASE_HOST} -U datadog postgres -A \
-c "select * from pg_stat_statements limit 1;"\
&&echo -e "\e[0;32mPostgres pg_stat_statements read OK\e[0m"\
||echo -e "\e[0;31mCannot read from pg_stat_statements\e[0m"
When prompted for a password, use the password you created for the datadog user.
Install the Agent
Installing the Datadog Agent also installs the Postgres check, which is required for Database Monitoring on Supabase.
If you haven’t installed the Agent, see the Agent installation instructions. Then, return here to continue with the instructions for your installation method.
Supabase’s default direct connection string is only valid on IPv6 networks.
To connect the Agent to a Supabase instance using this method, ensure that the machine running the Agent is IPv6 enabled. Reference your cloud provider’s documentation for more information.
Supabase instances on the Pro plan or above support IPv4 addresses as an add-on.
Edit the Agent’s conf.d/postgres.d/conf.yaml file to point to the Supabase instance you want to monitor. For a complete list of configuration options, see the sample postgres.d/conf.yaml.
init_config:instances:- dbm:truehost:<SUPABASE_INSTANCE_ENDPOINT>port:5432username:datadogpassword:'ENC[datadog_user_database_password]'## Optional: Connect to a different database if needed for `custom_queries`# dbname: '<DB_NAME>'
Note: If your password includes special characters, wrap it in single quotes.
Although we recommend having a direct connection to the database instead of connecting via proxy, you can still connect the Agent to your Supabase instance if the above options are not available to you. This will work best when you only have one instance in your Supabase project.
Get the session pooler connection string for your project via the Connect dialog, and copy it into your Agent configuration file:
It is common to configure a single Agent host to connect to multiple remote database instances (see Agent installation architectures for DBM). To connect to multiple hosts, create an entry for each host in the Postgres integration config.
Datadog recommends using one Agent to monitor no more than 30 database instances.
Benchmarks show that one Agent running on a t4g.medium EC2 instance (2 CPUs and 4GB of RAM) can successfully monitor 30 RDS db.t3.medium instances (2 CPUs and 4GB of RAM).
Use the database_autodiscovery option to permit the Agent to discover all databases on your host to monitor. You can specify include or exclude fields to narrow the scope of databases discovered. See the sample postgres.d/conf.yaml for more details.
init_config:instances:- dbm:truehost:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'database_autodiscovery:enabled:true# Optionally, set the include field to specify# a set of databases you are interested in discoveringinclude:- mydb.*- example.*tags:- 'env:prod'- 'team:team-discovery'- 'service:example-service'
Running custom queries
To collect custom metrics, use the custom_queries option. See the sample postgres.d/conf.yaml for more details.
init_config:instances:- dbm:truehost:localhostport:5432username:datadogpassword:'ENC[datadog_user_database_password]'custom_queries:- metric_prefix:employeequery:SELECT age, salary, hours_worked, name FROM hr.employees;columns:- name:custom.employee_agetype:gauge- name:custom.employee_salarytype:gauge- name:custom.employee_hourstype:count- name:nametype:tagtags:- 'table:employees'
Monitoring relation metrics for multiple databases
In order to collect relation metrics (such as postgresql.seq_scans, postgresql.dead_rows, postgresql.index_rows_read, and postgresql.table_size), the Agent must be configured to connect to each database (by default, the Agent only connects to the postgres database).
Specify a single “DBM” instance to collect DBM telemetry from all databases. Use the database_autodiscovery option to avoid specifying each database name.
init_config:instances:# This instance is the "DBM" instance. It will connect to the# all logical databases, and send DBM telemetry from all databases- dbm:truehost:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'database_autodiscovery:enabled:trueexclude:- ^users$- ^inventory$relations:- relation_regex:.*# This instance only collects data from the `users` database# and collects relation metrics from tables prefixed by "2022_"- host:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'dbname:usersdbstrict:truerelations:- relation_regex:2022_.*relkind:- r- i# This instance only collects data from the `inventory` database# and collects relation metrics only from the specified tables- host:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'dbname:inventorydbstrict:truerelations:- relation_name:products- relation_name:external_seller_products
Use the database_autodiscovery option to avoid specifying each logical database. See the sample postgres.d/conf.yaml for more details.
init_config:# This instance only collects data from the `users` database# and collects relation metrics only from the specified tablesinstances:- dbm:truehost:example-service-primary.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'dbname:usersdbstrict:truecollect_schemas:enabled:truerelations:- products- external_seller_products# This instance detects every logical database automatically# and collects relation metrics from every table- dbm:truehost:example-service–replica-1.example-host.comport:5432username:datadogpassword:'ENC[datadog_user_database_password]'database_autodiscovery:enabled:truecollect_schemas:enabled:truerelations:- relation_regex:.*
Working with hosts through a proxy
If the Agent must connect through a proxy such as the Cloud SQL Auth proxy, all telemetry is tagged with the hostname of the proxy rather than the database instance. Use the reported_hostname option to set a custom override of the hostname detected by the Agent.