How to Use PGBENCH to Test Database Performance on a Linux Machine

How to Use PGBENCH to Test Database Performance

When it comes to database performance, ensuring that your PostgreSQL database runs efficiently is crucial. Whether you’re running a shared hosting environment or a dedicated server, gauging the efficiency of database operations can help in optimizing the overall performance of your applications. One of the most popular tools for this purpose is pgbench.

pgbench is a benchmarking tool included with PostgreSQL that simulates client connections and runs tests to measure the performance of the database. This tool is especially useful for those who want to assess the efficiency of database operations and evaluate query performance and latency.

In this guide, we will show how to use pgbench on a Linux machine to test the performance of your PostgreSQL database. We’ll cover installation, basic usage, and advanced testing scenarios. By the end, you’ll have a comprehensive understanding of how to utilize this tool to its fullest potential.

Let’s get started.

1. Installing `pgbench`

Before you can use pgbench, you need to have PostgreSQL installed on your Linux machine. pgbench is typically included in the PostgreSQL package.

Install PostgreSQL:

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

Verify Installation:

After installation, you can check if pgbench is installed by running:

pgbench --version

2. Basic Usage of `pgbench`

To start with, you’ll want to initialize a test database with pgbench.

Initialize the Database:

pgbench -i -U your_username your_database_name


dropping old tables...
NOTICE:  table "pgbench_accounts" does not exist, skipping
NOTICE:  table "pgbench_branches" does not exist, skipping
NOTICE:  table "pgbench_history" does not exist, skipping
NOTICE:  table "pgbench_tellers" does not exist, skipping
creating tables...
generating data (client-side)...
100000 of 100000 tuples (100%) done (elapsed 0.23 s, remaining 0.00 s)
creating primary keys...
done in 0.89 s (drop tables 0.02 s, create tables 0.03 s, client-side generate 0.23 s, vacuum 0.38 s, primary keys 0.23 s).

Run a Simple Benchmark Test:

pgbench -c 10 -t 1000 -U your_username your_database_name

Here, -c stands for the number of clients, and -t is the number of transactions per client.

See also  How to Use ‘ioping’ to Measure the Speed of Data Reads/Writes on Storage Devices in Linux


transaction type: 
scaling factor: 1
query mode: simple
number of clients: 10
number of threads: 1
number of transactions per client: 1000
number of transactions actually processed: 10000/10000
latency average = 21.34 ms
tps = 468.29 (including connections establishing)
tps = 472.13 (excluding connections establishing)

3. Advanced Testing with `pgbench`

pgbench offers a variety of options to customize your tests:

You can use custom SQL scripts instead of the default ones provided by pgbench.

pgbench -f /path/to/your/script.sql -U your_username your_database_name

Instead of specifying the number of transactions, you can specify a duration for the test.

pgbench -c 10 -T 60 -U your_username your_database_name

Here, -T specifies the duration in seconds.

If you want to perform a read-only test without any write operations:

pgbench --select-only -U your_username your_database_name

Commands Mentioned

  • pgbench -i – Initializes a test database for pgbench.
  • pgbench -c – Specifies the number of clients for the test.
  • pgbench -t – Specifies the number of transactions per client.
  • pgbench -f – Uses a custom SQL script for the test.
  • pgbench -T – Specifies the duration of the test in seconds.
  • pgbench –select-only – Performs a read-only test.


  1. What is the primary purpose of `pgbench`?

    `pgbench` is a benchmarking tool included with PostgreSQL designed to simulate client connections and measure the performance of the database. It helps in assessing the efficiency of database operations and evaluating query performance and latency.

  2. How do I specify the number of clients in `pgbench`?

    You can specify the number of clients using the `-c` option followed by the desired number. For example, `pgbench -c 10` will run the test with 10 clients.

  3. Can I run a read-only test with `pgbench`?

    Yes, you can perform a read-only test using the `–select-only` option. This will ensure that only read operations are performed without any write operations.

  4. How do I use a custom SQL script with `pgbench`?

    You can use a custom SQL script by specifying the `-f` option followed by the path to your script. For example, `pgbench -f /path/to/your/script.sql` will run the test using the provided SQL script.

  5. Is `pgbench` specific to PostgreSQL?

    Yes, `pgbench` is a tool that comes with PostgreSQL and is specifically designed to benchmark PostgreSQL databases.


Benchmarking is an essential aspect of database management. It allows administrators and developers to understand the performance characteristics of their databases and make informed decisions about optimizations. pgbench is a powerful tool in the PostgreSQL suite that offers a comprehensive set of features to test database performance. By understanding and utilizing its capabilities, you can ensure that your PostgreSQL database operates at its peak efficiency, whether it’s hosted on a VPS server, cloud hosting environment, or any other hosting solution.

Regularly using pgbench can provide insights into how different workloads affect your database’s performance. It’s especially beneficial when making changes to your database schema, queries, or underlying infrastructure. By benchmarking before and after such changes, you can quantitatively measure the impact of your modifications.

Moreover, as your application grows and evolves, so will its database requirements. Regular benchmarking can help identify potential bottlenecks or areas of inefficiency that might not have been apparent during the initial stages of development. This proactive approach ensures that you’re always ahead of performance issues, rather than reacting to them after they’ve affected your users.

In the ever-evolving world of web technologies, where user experience is paramount, ensuring that your database responds quickly and efficiently to requests is crucial. Tools like pgbench are invaluable in this pursuit, providing a clear picture of database performance and areas for improvement.

To get the most out of pgbench, it’s recommended to run tests in various scenarios, simulating different levels of user activity and database load. This comprehensive testing approach will provide a holistic view of your database’s performance, allowing for targeted optimizations.

In conclusion, pgbench is more than just a benchmarking tool; it’s a window into the inner workings of your PostgreSQL database. By leveraging its capabilities, webmasters and database administrators can ensure that their databases are not just functional, but optimized for peak performance, regardless of whether they’re running on a Nginx server, an Apache setup, or the high-speed LiteSpeed platform. Regular benchmarking with pgbench is a step towards a faster, more efficient, and user-friendly application.


Leave a Reply

Your email address will not be published. Required fields are marked *