{"id":18962,"date":"2022-07-14T09:09:28","date_gmt":"2022-07-14T09:09:28","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=18962"},"modified":"2023-10-14T09:26:42","modified_gmt":"2023-10-14T09:26:42","slug":"how-to-use-pgbench-to-test-database-performance-on-a-linux-machine","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-use-pgbench-to-test-database-performance-on-a-linux-machine\/","title":{"rendered":"How to Use PGBENCH to Test Database Performance on a Linux Machine"},"content":{"rendered":"<p><img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance-1024x768.jpg\" alt=\"How to Use PGBENCH to Test Database Performance\" width=\"1024\" height=\"768\" class=\"alignnone size-large wp-image-18963 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance-1024x768.jpg 1024w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance-300x225.jpg 300w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance-128x96.jpg 128w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance-420x315.jpg 420w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance-540x405.jpg 540w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance-720x540.jpg 720w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance-960x720.jpg 960w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance-1140x855.jpg 1140w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance-1320x990.jpg 1320w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/10\/How-to-Use-PGBENCH-to-Test-Database-Performance.jpg 1400w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/768;\" \/><\/p>\n<p>When it comes to database performance, ensuring that your PostgreSQL database runs efficiently is crucial. Whether you&#8217;re running a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-shared-hosting\/\">shared hosting<\/a> environment or a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-dedicated-server-hosting\/\">dedicated server<\/a>, 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.<\/p>\n<p>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.<\/p>\n<p>In this guide, we will show how to use pgbench on a Linux machine to test the performance of your PostgreSQL database. We&#8217;ll cover installation, basic usage, and advanced testing scenarios. By the end, you&#8217;ll have a comprehensive understanding of how to utilize this tool to its fullest potential.<\/p>\n<p>Let&#8217;s get started.<\/p>\n<h2>1. Installing `pgbench`<\/h2>\n<p>Before you can use pgbench, you need to have PostgreSQL installed on your Linux machine. pgbench is typically included in the PostgreSQL package.<\/p>\n<p>Install PostgreSQL:<\/p>\n<pre>\r\nsudo apt-get update\r\nsudo apt-get install postgresql postgresql-contrib\r\n<\/pre>\n<p>Verify Installation:<\/p>\n<p>After installation, you can check if pgbench is installed by running:<\/p>\n<pre>\r\npgbench --version\r\n<\/pre>\n<h2>2. Basic Usage of `pgbench`<\/h2>\n<p>To start with, you&#8217;ll want to initialize a test database with pgbench.<\/p>\n<p>Initialize the Database:<\/p>\n<pre>\r\npgbench -i -U your_username your_database_name\r\n<\/pre>\n<p>Example:<\/p>\n<pre>\r\ndropping old tables...\r\nNOTICE:  table \"pgbench_accounts\" does not exist, skipping\r\nNOTICE:  table \"pgbench_branches\" does not exist, skipping\r\nNOTICE:  table \"pgbench_history\" does not exist, skipping\r\nNOTICE:  table \"pgbench_tellers\" does not exist, skipping\r\ncreating tables...\r\ngenerating data (client-side)...\r\n100000 of 100000 tuples (100%) done (elapsed 0.23 s, remaining 0.00 s)\r\nvacuuming...\r\ncreating primary keys...\r\ndone 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).\r\n<\/pre>\n<p>Run a Simple Benchmark Test:<\/p>\n<pre>\r\npgbench -c 10 -t 1000 -U your_username your_database_name\r\n<\/pre>\n<p>Here, -c stands for the number of clients, and -t is the number of transactions per client.<\/p>\n<p>Example:<\/p>\n<pre>\r\ntransaction type: <builtin: TPC-B (sort of)>\r\nscaling factor: 1\r\nquery mode: simple\r\nnumber of clients: 10\r\nnumber of threads: 1\r\nnumber of transactions per client: 1000\r\nnumber of transactions actually processed: 10000\/10000\r\nlatency average = 21.34 ms\r\ntps = 468.29 (including connections establishing)\r\ntps = 472.13 (excluding connections establishing)\r\n<\/pre>\n<h2>3. Advanced Testing with `pgbench`<\/h2>\n<p>pgbench offers a variety of options to customize your tests:<\/p>\n<p>You can use custom SQL scripts instead of the default ones provided by pgbench.<\/p>\n<pre>\r\npgbench -f \/path\/to\/your\/script.sql -U your_username your_database_name\r\n<\/pre>\n<p>Instead of specifying the number of transactions, you can specify a duration for the test.<\/p>\n<pre>\r\npgbench -c 10 -T 60 -U your_username your_database_name\r\n<\/pre>\n<p>Here, -T specifies the duration in seconds.<\/p>\n<p>If you want to perform a read-only test without any write operations:<\/p>\n<pre>\r\npgbench --select-only -U your_username your_database_name\r\n<\/pre>\n<h2>Commands Mentioned<\/h2>\n<ul>\n<li><span class=\"fw-bold\">pgbench -i<\/span> \u2013 Initializes a test database for pgbench.<\/li>\n<li><span class=\"fw-bold\">pgbench -c<\/span> \u2013 Specifies the number of clients for the test.<\/li>\n<li><span class=\"fw-bold\">pgbench -t<\/span> \u2013 Specifies the number of transactions per client.<\/li>\n<li><span class=\"fw-bold\">pgbench -f<\/span> \u2013 Uses a custom SQL script for the test.<\/li>\n<li><span class=\"fw-bold\">pgbench -T<\/span> \u2013 Specifies the duration of the test in seconds.<\/li>\n<li><span class=\"fw-bold\">pgbench &#8211;select-only<\/span> \u2013 Performs a read-only test.<\/li>\n<\/ul>\n<h2>FAQ<\/h2>\n<ol itemscope itemtype=\"https:\/\/schema.org\/FAQPage\">\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is the primary purpose of `pgbench`?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">`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.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How do I specify the number of clients in `pgbench`?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">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.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">Can I run a read-only test with `pgbench`?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">Yes, you can perform a read-only test using the `&#8211;select-only` option. This will ensure that only read operations are performed without any write operations.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">How do I use a custom SQL script with `pgbench`?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">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.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">Is `pgbench` specific to PostgreSQL?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">Yes, `pgbench` is a tool that comes with PostgreSQL and is specifically designed to benchmark PostgreSQL databases.<\/span>\n            <\/p>\n<\/li>\n<\/ol>\n<h2>Conclusion<\/h2>\n<p>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&#8217;s hosted on a <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-vps-hosting\/\">VPS server<\/a>, <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-cloud-hosting\/\">cloud hosting<\/a> environment, or any other hosting solution.<\/p>\n<p>Regularly using pgbench can provide insights into how different workloads affect your database&#8217;s performance. It&#8217;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.<\/p>\n<p>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&#8217;re always ahead of performance issues, rather than reacting to them after they&#8217;ve affected your users.<\/p>\n<p>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.<\/p>\n<p>To get the most out of pgbench, it&#8217;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&#8217;s performance, allowing for targeted optimizations.<\/p>\n<p>In conclusion, pgbench is more than just a benchmarking tool; it&#8217;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&#8217;re running on a <a href=\"https:\/\/webhostinggeeks.com\/blog\/nginx-server-explained\/\">Nginx<\/a> server, an <a href=\"https:\/\/webhostinggeeks.com\/blog\/apache-http-server-explained\/\">Apache<\/a> setup, or the high-speed <a href=\"https:\/\/webhostinggeeks.com\/blog\/litespeed-web-server-explained\/\">LiteSpeed<\/a> platform. Regular benchmarking with pgbench is a step towards a faster, more efficient, and user-friendly application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it comes to database performance, ensuring that your PostgreSQL database runs efficiently is crucial. Whether you&#8217;re running a shared hosting environment or a dedicated server, gauging the efficiency of&#8230;<\/p>\n","protected":false},"author":6,"featured_media":18963,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[2152],"tags":[2186,2153],"class_list":["post-18962","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-benchmarking","tag-pgbench","tag-test"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/18962","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=18962"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/18962\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/18963"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=18962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=18962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=18962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}