2

I am working as a Postgres DBA at my organization. We are currently working on Postgres 9.5 on SUSE Linux servers. I wanted a specific solution. We have multiple SUSE Linux servers and each server can host one or more Postgres database. My requirement is I need to find the list of all the database available on a particular server irrespective of the database is up and running or its shut down.

Is there any file or any location where Postgres keeps note of all the databases that is created on a server. Is there a way I can get the required details without connecting to the Postgres instance and without running any PSQL commands?

If not what would be the best way to get the list. Any hints, solutions and thoughts would help me to resolve this issue.

Thanks a lot for the help in advance.

Praveen
  • 31
  • 2
  • 3
  • 1
    When you say "instance", do you mean there may be more than one Postgres daemon running on a server (more than one "cluster", in Postgres terminology), or just that there may be more than one database configured within the cluster? – IMSoP Mar 22 '17 at 10:43

1 Answers1

0

In PostgreSQL, a server process serves a database cluster, which physically resides in a data directory.

A PostgreSQL cluster contains several databases, all of which are shut down when the server process is shut down.

So your question can be split into two parts:

  1. How to find out which database clusters are on a machine, regardless if they are started or not?

  2. How to find out all databases inside a cluster even if the cluster is shut down?

There is no generic answer to the first question, since a data directory could in principle be everywhere and could belong to any user.

You could get an approximation with

find / -name PG_VERSION

but it is probably better to find a special solution that fits your setup (about which you didn't tell us).

The second question is even harder.

To even get a list of numeric IDs, you would have to know all tablespace directories. Those in the default tablespace are the names of the subdirectories in the base subdirectory of the data directory.

In order to get the names of the databases you have to have the PostgreSQL server started, because it would be very hard to find the file that belongs to pg_database and parse it.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263