Is postgres database always available in PostgreSQL?

I am writing a script that needs to get all databases in a postgres cluster and therefore needs to connect to a database (it doesn't matter what for).

Is it safe to assume that the postgres database will always be connectable, or if not, is there a programmatic way to define an arbitrary database in the cluster that I can connect to?

+3


source to share


2 answers


PostgreSQL has three "system" databases: postgres, template0, and template1. I'm sure they can all be removed. (You might want to test this in a virtual machine.) I'm pretty sure postgres and template1 can be removed by root user. By default, template0 does not accept connections.

Having said that, the main purpose of "postgres" is to provide you with a database to connect to .



The postgres database is also created when the database cluster is initialized. This database is used as the default database for users and the application to connect. It's just a copy of the template1 and can be dropped and recreated if needed.

So this is a reasonable assumption, but not a bulletproof assumption. I can imagine some paranoid (cautious) stores might lose "postgres" and use a different database as their default internal if their server was compromised. This is similar to using a non-standard port for SSH.

+2


source


In my personal experience, yes, it almost guaranteed it would exist. But, according to the official PostgreSQL documentation:

The database server itself does not require the postgres database to exist, 
but many external utility programs assume it exists.

      



So, you cannot assume that it will always be available, although in practice it will almost certainly be available.

+2


source







All Articles