Question: How can you list all clusters in a PostgreSQL installation?
Answer
In PostgreSQL, the term 'cluster' refers to a collection of databases that are managed by a single instance of a PostgreSQL server. A cluster includes the shared configuration and data storage for those databases. On most operating systems, PostgreSQL service or daemon manages only one cluster, but it's possible to configure multiple clusters on a single machine, each running under separate server processes with their own settings and data storage directories.
To manage and view clusters in PostgreSQL, especially when managing multiple clusters on a single system, you typically use the pg_lsclusters
command, which is part of the postgresql-common
package available in Debian-based systems (like Ubuntu). This command provides detailed information about each cluster, including its version, name, port, status, owner, and data directory.
Here’s how you can use pg_lsclusters
:
pg_lsclusters
This will output something like the following:
Ver Cluster Port Status Owner Data directory Log file
12 main 5432 online postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
For systems not using postgresql-common
(such as non-Debian-based systems), listing clusters isn't standardized by a specific tool. In such cases, managing multiple clusters usually involves manually configuring different instances of the PostgreSQL service, each listening on different ports and/or using different data directories. You can check these configurations by inspecting the respective postgresql.conf
file for each service.
If you have administrative access, you can also use SQL queries within a PostgreSQL session to explore details about the current database cluster:
SELECT current_setting('data_directory');
This SQL command shows the data directory of the cluster connected to your current session.
Note: The ability to manage multiple clusters easily with tools like pg_lsclusters
is one of the reasons why PostgreSQL is popular for more complex setups requiring separation of database environments on a single server.
Was this content helpful?
Other Common PostgreSQL Questions (and Answers)
- How do you manage Postgres replication lag?
- How can I limit the number of rows updated in a PostgreSQL query?
- How does sharding work in PostgreSQL?
- How do you limit the number of rows deleted in PostgreSQL?
- How do you use the PARTITION OVER clause in PostgreSQL?
- What are PostgreSQL replication slots and how do they work?
- How can you partition an existing table in PostgreSQL?
- How do you partition a table by multiple columns in PostgreSQL?
- How do you check the replication status in PostgreSQL?
- What are the scaling limits of PostgreSQL?
- How do you scale Azure PostgreSQL?
- How do you use the limit clause in PostgreSQL to get the top N rows of a query result?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost