I created a script to drop a particular database and then reload just the structure. I created it using phppgsql.
I'm still new to pgsql, so I simply ran the script using \i myCreateScript.sql
with the psql
client. What happened was that it clobbered the default postgres
database, and now all databases are utterly unusable. I didn't figure out until later that I was supposed to do \c myDatabase
first to set the current database.
So.. What do I do now? I'm running on a linux server. Should I purge postgres and re-install? Is there another way to recover from this?
Thanks in advance.
2 Answers 2
Your question is very unclear (see comment) but I'm going to make a guess at it anyway.
I think you DROP
ped the postgres
database. You're used to always connecting to PostgreSQL as the postgres
superuser using the postgres
database, probably via sudo and peer
authentication with something like:
sudo -u postgres psql
where you don't specify a database name to connect to. So with no postgres
database, you don't know how to connect anymore. You get an error like:
psql: FATAL: database "postgres" does not exist
If so, you just need to connect to a different database and re-create the postgres
database. It only exists as a convenient scratch area - the system doesn't actually care if the postgres
database exists or not, and doesn't use it for anything.
Something like:
sudo -u postgres psql template1 -c 'CREATE DATABASE postgres;'
will do the trick.
If you dropped the template1
database as well, you could still recover by connecting to any other database on the server and re-creating template1
from the locked, protected template0
database; see the documentation on template databases.
Key things to understand:
You don't have to connect to the
postgres
database. It doesn't get used for anything except a default place to connect to when using thepostgres
user. If youDROP
it the system doesn't care.psql
connects by default to the database with the same name as the current user. You can just pick another one withpsql databasename
.psql
connects by default with the username of the current unix user. You can just pick another one with (eg)psql -U someuser
. This will work fine ifpg_hba.conf
is configured to permit the connection.While the
postgres
user is the default superuser, you can create another one, and you don't have to rely onpeer
authentication to use it either; you can usemd5
password auth or whatever you want. So while you're probably used to just usingsudo -u postgres psql
you can actually set things up in a variety of flexible ways. I frequently create a database superuser for my regular unix user account (CREATE USER myuser WITH SUPERUSER ENCRYPTED PASSWORD 'blah';
) when I'm on a scratch/development box, so I'm not messing about withsudo
all the time.
-
1This explained a lot and helped.. thank you for your time!Bill– Bill2013年12月31日 14:05:01 +00:00Commented Dec 31, 2013 at 14:05
-
Also, if you've somehow managed to drop all databases (which shouldn't be possible) you can still start PostgreSQL up in single-user recovery mode against
template0
.Craig Ringer– Craig Ringer2015年07月30日 07:37:56 +00:00Commented Jul 30, 2015 at 7:37
You might try connecting to another database in psql, dropping the postgres database, and simply recreating it, which should make a copy based on template1.
drop database postgres;
create database postgress;
-
Hm.., well I did this and all that happened is that I end up with an empty postgres database. I read the link, but I see nothing along the lines of
template1
anywhereB. VB.– B. VB.2013年12月29日 17:39:02 +00:00Commented Dec 29, 2013 at 17:39 -
The default postgres database is empty of relations. More documentation on templates here.banderkat– banderkat2013年12月29日 17:45:13 +00:00Commented Dec 29, 2013 at 17:45
postgres
database? Do you mean youDROP
ped it? And how exactly are all DBs unusable? Exact error messages and precise description of effects please. Droppingpostgres
doesn't affect the other databases, so it's not clear what the problem is. You also forgot to mention your PostgreSQL version.