2
votes

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.

3
Which linux distibution are you using? - Mladen Uzelac

3 Answers

5
votes

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.

1
votes

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

0
votes
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.