{{< img src="integrations/postgresql/pggraph.png" alt="PostgreSQL Graph" responsive="true" popup="true">}}
Get metrics from PostgreSQL service in real time to:
- Visualize and monitor PostgreSQL states
- Be notified about PostgreSQL failovers and events.
The PostgreSQL check is packaged with the Agent. To start gathering your PostgreSQL metrics and logs, install the Agent.
Edit the postgres.d/conf.yaml file, in the conf.d/ folder at the root of your Agent's directory to start collecting your PostgreSQL metrics and logs. See the sample postgres.d/conf.yaml for all available configuration options.
To get started with the PostgreSQL integration, create at least a read-only Datadog user with proper access to your PostgreSQL server. Start psql on your PostgreSQL database and run:
create user datadog with password '<PASSWORD>';
grant SELECT ON pg_stat_database to datadog;
Note: When generating custom metrics that require querying additional tables, you may need to grant the CONNECT permission on those tables to the datadog user.
To verify the correct permissions run the following command:
psql -h localhost -U datadog postgres -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"
When it prompts for a password, enter the one used in the first command.
-
Edit the
postgres.d/conf.yamlfile to point to your server and port, set the masters to monitor. See the sample postgres.d/conf.yaml for all available configuration options. Configuration Options:-
username(Optional) - The user account used to collect metrics, set in the Installation section above -
password(Optional) - The password for the user account. -
dbname(Optional) - The name of the database you want to monitor. -
ssl(Optional) - Defaults to False. Indicates whether to use an SSL connection. -
use_psycopg2(Optional) - Defaults to False. Setting this option toTruewill force the Datadog Agent to collect PostgreSQL metrics using psycopg2 instead of pg8000. Note that pyscopg2 does not support SSL connections. -
tags(Optional) - A list of tags applied to all metrics collected. Tags may be simple strings or key-value pairs. -
relations(Optional) - By default, all schemas are included. Add specific schemas here to collect metrics for schema relations. Each relation will generate 10 metrics and an additional 10 metrics per index. Use the following structure to declare relations:relations: - relation_name: my_relation schemas: - my_schema_1 - my_schema_2 -
collect_function_metrics(Optional) - Collect metrics regarding PL/pgSQL functions from pg_stat_user_functions -
collect_count_metrics(Optional) - Collect count metrics. The default value isTruefor backward compatibility, but this might be slow. The recommended value isFalse.
-
-
Restart the Agent to start sending PostgreSQL metrics to Datadog.
PostgreSQL default logging is to stderr and logs do not include detailed information. This is why we suggest to log into a file with additional details specified in the log line prefix.
-
Edit your PostgreSQL configuration file
/etc/postgresql/<version>/main/postgresql.confand uncomment the following parameter in the log section:logging_collector = on log_directory = 'pg_log' # directory where log files are written, # can be absolute or relative to PGDATA log_filename = 'pg.log' #log file name, can include pattern log_statement = 'all' #log all queries log_line_prefix= ‘%m [%p] %d %a %u %h %c ‘ log_file_mode = 0644 ## For Windows #log_destination = ‘eventlog’ -
Collecting logs is disabled by default in the Datadog Agent, you need to enable it in datadog.yaml:
logs_enabled: true -
Add this configuration setup to your
postgres.d/conf.yamlfile to start collecting your PostgreSQL logs:
logs:
- type: file
path: /var/log/pg_log/pg.log
source: postgresql
sourcecategory: database
service: myapp
#To handle multi line that starts with yyyy-mm-dd use the following pattern
#log_processing_rules:
# - type: multi_line
# pattern: \d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])
# name: new_log_start_with_date
Change the service and path parameter values and configure them for your environment.
See the sample postgres.d/conf.yaml for all available configuration options.
Learn more about log collection on the log documentation
Run the Agent's status subcommand and look for postgres under the Checks section.
Some of the metrics listed below require additional configuration, refer to the sample postgres.d/conf.yaml for all configurable options.
See metadata.csv for a list of metrics provided by this integration.
The PostgreSQL check does not include any event at this time.
postgres.can_connect
Returns CRITICAL if the Agent is unable to connect to the monitored PostgreSQL instance. Returns OK otherwise.
To get a better idea of how (or why) to have 100x faster PostgreSQL performance by changing 1 line with Datadog, check out our series of blog posts about it.
The Agent generates PostgreSQL metrics from custom query results. For each custom query, four components are required: descriptors, metrics, query, and relation.
queryis where you'll construct a base SELECT statement to generate your custom metrics. Each column name in your SELECT query should have a corresponding item in thedescriptorssection. Each item inmetricswill be substituted for the first%sin the query.metricsare key-value pairs where the key is the query column name or column function and the value is a tuple containing the custom metric name and metric type (RATE,GAUGE, orMONOTONIC). In the example below, the results of the sum of theidx_scancolumn will appear in Datadog with the metric namepostgresql.idx_scan_count_by_table.descriptorsis used to add tags to your custom metrics. It's a list of lists each containing 2 strings. The first string is for documentation purposes and should be used to make clear what you are getting from the query. The second string will be the tag name. For multiple tags, include additional columns in yourquerystring and a corresponding item in thedescriptors. The order of items indescriptorsmust match the columns inquery.relationindicates whether to include schema relations specified in therelationsconfiguration option. If set totrue, the second%sinquerywill be set to the list of schema names specified in therelationsconfiguration option.
custom_metrics:
# All index scans & reads
- descriptors:
- [relname, table]
- [schemaname, schema]
metrics:
SUM(idx_scan) as idx_scan_count: [postgresql.idx_scan_count_by_table, RATE]
SUM(idx_tup_read) as idx_read_count: [postgresql.idx_read_count_by_table, RATE]
query: SELECT relname, schemaname, %s FROM pg_stat_all_indexes GROUP BY relname;
relation: false
The example above will run two queries in PostgreSQL:
SELECT relname, SUM(idx_scan) as idx_scan_count FROM pg_stat_all_indexes GROUP BY relname;will generate a rate metricpostgresql.idx_scan_count_by_table.SELECT relname, SUM(idx_tup_read) as idx_read_count FROM pg_stat_all_indexes GROUP BY relname;will generate a rate metricpostgresql.idx_read_count_by_table.
Both metrics will use the tags table and schema with values from the results in the relname and schemaname columns respectively. e.g. table: <relname>
The postgres.yaml.example file includes an example for the SkyTools 3 Londoniste replication tool:
custom_metrics:
# Londiste 3 replication lag
- descriptors:
- [consumer_name, consumer_name]
metrics:
GREATEST(0, EXTRACT(EPOCH FROM lag)) as lag: [postgresql.londiste_lag, GAUGE]
GREATEST(0, EXTRACT(EPOCH FROM lag)) as last_seen: [postgresql.londiste_last_seen, GAUGE]
pending_events: [postgresql.londiste_pending_events, GAUGE]
query:
SELECT consumer_name, %s from pgq.get_consumer_info() where consumer_name !~ 'watermark$';
relation: false
Run the Agent's status subcommand and look for postgres under the Checks section:
postgres
--------
- instance #0 [ERROR]: 'Missing relation parameter in custom metric'
- Collected 0 metrics, 0 events & 0 service checks
You should also check the /var/log/datadog/collector.log file for more information.