3

I have a funny question about PostgreSQL database: What happens if the postgres database is dropped?

dropdb postgres worked.
createdb postgres worked too.
psql worked.

But I thought the users would be lost. Yet the old users are still there.

So where are the users stored for the database and which implications does dropping the postgres database have?

2 Answers 2

4

PostgreSQL metadata are stored in catalog tables, which are in the pg_catalog schema. These are accessible like regular views and tables.

There are shared system catalog tables which are shared between all databases. These tables are not affected when databases are dropped.

pg_authid, the table where the users are stored, is one of those shared catalogs. This is because in PostgreSQL, users don't belong to a database, but to the whole database cluster.

You can list all shared catalog tables like this:

SELECT relname FROM pg_class
WHERE relisshared AND relkind = 'r';

In the documentation you can find more information about the system catalogs.

Sign up to request clarification or add additional context in comments.

1 Comment

I took the liberty to turn it into a real answer. Rollback if you don't approve.
4

When connecting to a Postgres server, you always need to specify which database you want to connect to.

When you set up a new server, you need something to connect to before you can run your first CREATE DATABASE statement.

That's all the postgres database is: a dummy database to use as a connection target for admin commands. There's no data in there, and you're free to drop it and use a different one instead (though whoever inherits your system will probably not thank you for it...).

As gil.fernandes said in his answer, server-wide objects like users are accessible from every database, but aren't stored inside any database in particular.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.