Home > Back-end >  How to add Database to default server in Postgres, if all databases are deleted?
How to add Database to default server in Postgres, if all databases are deleted?

Time:09-14

I ran and new node application that used a Postgres SQL server. It created a default database called postgres and I wanted to remove it.

I used the below command and close the connection and was able to remove that database.

SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'postgres';

Now the pgAdmin looks like the below image.

enter image description here

I am not sure how to add a database again here. It always gives me errors while adding a new server that Database "postgres" does not exist. Any ideas or search query to google about this issue?

CodePudding user response:

You are connecting as user "postgres" and the default database name it will try is also "postgres". You should be able to specify a different database in the pgadmib settings though. Try "template0" and connect to that to create a new db.

CodePudding user response:

I am tempted to say that if you are randomly deleting system databases without first researching if they are needed, you deserve what you get. But while it is customary to leave the postgres database as default database for tools to connect to, it is certainly not forbidden to do it.

The solution in your case is probably to reconfigure your pgAdmin connection to use template1 instead of postgres as "maintenance database". The only problem with that (and the main reason why the postgres database was introduced) is that it will prevent you from creating new databases with template1 as the (default) template if others are connected to that database as well.

  • Related