2

I'm trying to create db on PostgreSQL on linux machine. And I use following command to create the DB.

createdb mydbname;

This is throwing following error.

createdb: could not connect to database postgres: FATAL: database "postgres" does not exist

I'm new to PostgreSQL. Can you please guide me to go through this error? Simply I want to create a DB on psql.

1
  • Which linux distibution are you using? Commented Nov 20, 2014 at 14:54

3 Answers 3

5

It looks like you dropped the postgres database. It's the default used for many admin operations, so you might want to re-create it:

createdb --maintenance-db=template1 -T template0 postgres;

What that's doing is connecting to the built-in DB template1 in order to create the db postgres by making a copy of template0.

Once that's done, your command:

createdb mydbname;

will work as normal.

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

6 Comments

This is giving following error createdb: illegal option -- d Try "createdb --help" for more information.
Then I change the parameter -d to -D. After that it gave another error like follows createdb: database creation failed: ERROR: syntax error at or near "TABLESPACE" at character 26
I edit your command like follows createdb -T template1 postgres; it mean change the -d to -D. Then my issue is fixed. Was that the the command you wanted to execute?
Can you change it to -T. The solution is working fine. then I can mark it as correct answer.
if you change -D to -T on the answer I can make it as green. It will be useful to others.
|
1

su - postgres change user to postgres
psql - run psql terminal
postgres# create database somedatabase create database;
postgres#\c somedatabase connecto to database
somedatabase# your are connected to that database as postgres user

5 Comments

Here also second command psql give error like follows (psql: FATAL: database "postgres" does not exist)
did you start the server? sudo service postgresql start or alike
It is already running. Even I can use other already existing DB's.
psql -U postgres -d existingdb (will connect to terminal with user postgres and with), and then run create database newdb. It seems that postgres database is erased!!???. I assume you didn't erase it.
run psql -U postgres -l to list all the databases. You should have postgres database.
0
su - postgres change user to postgres
createdb
createdb somedatabase
psql somedatabase

The first line changes to the postgres system user. The second line creates a database for the postgres admin user. The third line creates the database you were trying to make. The fourth line connects to it as the postgres user.

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.