how-to-configure-a-postgresql-datasource-in-grafana

How To Configure a PostgreSQL Datasource in Grafana

Table of Contents

Introduction 

So, you’ve got a PostgreSQL database packed with juicy data, and you want to turn those raw numbers into slick, interactive Grafana dashboards? Good call! Grafana’s PostgreSQL datasource is like the secret handshake that lets you visualize your data in style—no extra ETL magic required. In this guide, we’ll walk through getting PostgreSQL and Grafana to play nice, covering everything from connection settings to query tuning. Whether you’re debugging performance metrics or just flexing your SQL muscles, by the end of this, you’ll have a fully operational setup. Let’s dive in!

This guide covers installing PostgreSQL, allowing remote connections, and configuring Grafana to visualize live data. Don't have Grafana yet? Sign up for MetricFire's free trial to try it out and follow along with this tutorial!

Install and Configure PostgreSQL

It's no secret that PG is a popular database among developers, and we're all about keeping things PG here! This guide helps you quickly set up PostgreSQL for external connections without requiring SSL certificates. While allowing all connections can simplify initial setup, it is best practice to secure your database with SSL to prevent unauthorized access and data interception. For detailed steps on configuring PG with SSL encryption and certificate-based authentication, check out our separate guide HERE.

This article also assumes that you already have an instance of PostgreSQL up and running. If not, we've included some steps on how to install/configure/run this on your own server (Linux):

  • sudo apt update
  • sudo apt install postgresql postgresql-contrib -y
  • sudo systemctl enable --now postgresql

Create a DB and User

Login: sudo -u postgres psql


CREATE DATABASE testdb; CREATE USER grafana_user WITH ENCRYPTED PASSWORD 'your_secure_password'; GRANT ALL PRIVILEGES ON DATABASE testdb TO grafana_user;

Allow Remote Connections

To allow PostgreSQL to accept connections from any IP address, modify the file typically located at: /etc/postgresql/<version>/main/postgresql.conf to reflect the following change:


listen_addresses = '*'

Confirm the changes were applied:

  • sudo -u postgres psql -c "SHOW listen_addresses;"

To ensure that Grafana can connect using the username and PW created in the first steps, modify the host based authentication file typically located at: /etc/postgresql/<version>/main/pg_hba.conf


host testdb grafana_user 0.0.0.0/0 md5

Restart PostgreSQL for changes to take effect

  • sudo systemctl restart postgresql
  • sudo systemctl status postgresql
Additionally, you can allow firewall access to port 5432 in your PG server and confirm the changes:
  • sudo ufw allow 5432/tcp
  • sudo ufw reload
  • sudo netstat -tulnp | grep postgres
  • sudo ss -lntp | grep 5432

Insert Sample Data

Login to PG: sudo -u postgres psql

Switch to your example database: \c testdb;

Confirm you are in the correct db and instert some data:


SELECT current_database();

CREATE TABLE public.sample_data (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    value INT NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
);

INSERT INTO public.sample_data (name, value, created_at) VALUES ('CPU Load', floor(random() * 100), NOW()), ('Memory Usage', floor(random() * 15500) + 500, NOW()), ('Disk Space', floor(random() * 490) + 10, NOW());
Now you're ready to configure a PostgreSQL datasource in Grafana. And remember, you can test this out for free if you start a 14-day FREE trial with MetricFire's Hosted Grafana!

Configure a Grafana PostgreSQL Data Source

  1. Go to: Grafana → Configuration → Data Sources → PostgreSQL
  2. Enter Details:
    • Host<PostgreSQL_Server_IP>:5432
    • Databasetestdb
    • Usergrafana_user
    • Passwordyour_secure_password
  3. Set SSL Mode: disable
  4. Click "Save & Test"

How To Configure a PostgreSQL Datasource in Grafana - 1

Again, when allowing remote connections to a production DB, you should always use SSL certificates and should reference the related article HERE.

Create a Grafana Dashboard Panel

In your instance of Grafana, go to → Dashboards → New Dashboard → "Add a new panel"

Select your new PostgreSQL Datasource, set format as Time Series, select the Code option, and enter a Query for your sample data:


SELECT created_at AS "time", name AS "metric", value AS "value" FROM public.sample_data WHERE created_at >= now() - interval '5 minutes' ORDER BY created_at;

Then Save your changes by clicking Apply.

How To Configure a PostgreSQL Datasource in Grafana - 2

Conclusion

Boom! You’ve just leveled up your PostgreSQL game by making it remote-friendly, and Grafana-ready—no more rogue queries streaking across the internet in plain text. Now, with a few clicks, you can visualize database trends, track performance metrics, and flex your data skills like a true dashboard ninja. Whether you're monitoring system health, tracking app usage, or just love watching numbers dance in Grafana, this setup gives you real-time insights with zero guesswork. So why wait? Spin it up, graph it out, and make your data work for you! 

Sign up for the free trial and begin monitoring your infrastructure today. You can also book a demo and talk to the MetricFire team directly about your monitoring needs.

You might also like other posts...
metricfire Jul 08, 2025 · 5 min read

Introducing MetricFire Logging: Visualize Logs Alongside Metrics

By combining logs and metrics in MetricFire's Hosted Grafana, you can troubleshoot faster, spot... Continue Reading

metricfire Jul 01, 2025 · 3 min read

ログをメトリクスに変換: Loki、Python、Telegrafによるオープンソースの構築(前編)

ELKスタックを使用せず、最小限の労力で、軽量でオープンソースのセットアップを使用して、生のログを使用可能なメトリクスに変換する方法を紹介します。Loki、Python、Telegrafを使用して、ログをGraphiteメトリクスに変換し、簡単に監視やアラートを出すことができます。システム管理者、DevOps初心者、またはゼロからより革新的なモニタリングパイプラインを構築することに興味がある方に最適です。 Continue Reading

metricfire Jul 01, 2025 · 5 min read

OpenTelemetryを使用してSnowflakeの観測性を高める方法

このガイドでは、OpenTelemetry(contrib)をSnowflakeに統合して、クエリのパフォーマンス、実行時間、キューイング、ストレージの使用率、課金への影響を可視化する方法を説明します。SnowflakeメトリクスをスクレイピングするためのOpenTelemetry Collectorをセットアップし、データをエクスポートするためのエンドポイントを設定し、よくある落とし穴のトラブルシューティングを行います。最後には、クエリとリソースのパフォーマンスに関する実用的な洞察を提供する、Snowflake用のスケーラブルで自動化されたモニタリングの設定ができるようになります。 Continue Reading

header image

We strive for 99.999% uptime

Because our system is your system.

14-day trial 14-day trial
No Credit Card Required No Credit Card Required