2

Following the postgresql installation instructions for Mac, I recently created a db and launched the server. Everything looks like it's working fine.

/opt/local/lib/postgresql93/bin/postgres -D /opt/local/var/db/postgresql93/defaultdb
LOG:  database system was shut down at 2013-08-12 15:36:09 PDT
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

However, when I try to access the database from Python3 Django, I get the following error:

OperationalError: FATAL:  database "/opt/local/var/db/postgresql93/defaultdb" does not exist

If I go into that directory, defaultdb, I see that it exists and there are many files in it.

Aside from the above error message appearing in the Python traceback, it also appears in the postgres log:

FATAL:  database "defaultdb" does not exist
FATAL:  database "/opt/local/var/db/postgresql93/defaultdb" does not exist

I've also tried replacing the full path with just the name "defaultdb", but get the same message.

EDIT: In fact, running the following doesn't work either:

/opt/local/bin/psql93 defaultdb
psql93: FATAL:  database "defaultdb" does not exist
2
  • 2
    Does /opt/local/bin/psql93 --list show "defaultdb" in the output? Commented Aug 13, 2013 at 0:00
  • Thanks. I see that I don't have defaultdb show up. However, I do have "postgres", "template0", and "template1" as db's. Accessing "postgres" works. I've also noticed that createdb defaultdb fixes this. What I did before was sudo su postgres -c '/opt/local/lib/postgresql93/bin/initdb -D /opt/local/var/db/postgresql93/defaultdb' Commented Aug 13, 2013 at 0:14

2 Answers 2

7

initdb creates a cluster that is a global data directory (the path after the -D). This data directory is a container for a collection of databases plus the information shared across the databases (like the table of users or the database journal files). There is only one cluster per PostgreSQL installation but as many databases as you want inside that cluster.

createdb creates a database inside the cluster but it does not manipulate directly the file system, rather it issues a CREATE DATABASE SQL statement after being connected to the server. Databases are not refered to by paths but by names, contrary to the global data directory that designates a path on disk.

Special databases named template0, template1 and often postgres are automatically created with a new cluster but you don't want to store your application data into them.

It seems that your misunderstanding was believing that creating the cluster created the database at the same time or not knowing the distinction between both. Generally the cluster is automatically set up by the installation phase whereas creating the databases is left to the user.

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

Comments

-1

The following instructions are given after I installed postgresql93-server via Macports:

To create a database instance, after install do
 sudo mkdir -p /opt/local/var/db/postgresql93/defaultdb
 sudo chown postgres:postgres /opt/local/var/db/postgresql93/defaultdb
 sudo su postgres -c '/opt/local/lib/postgresql93/bin/initdb -D
/opt/local/var/db/postgresql93/defaultdb'

The above did not work for me and I don't see defaultdb show up in /opt/local/bin/psql93 --list

However, createdb defaultdb works and I immediately see defaultdb created.

2 Comments

The path after the "-D" is the $PGDATA directory. Under that path is where all the Postgresql data files etc are created. It is unrelated to your database name, and would be more reasonably associated with the Postgresql "cluster". One cluster can contain many databases, with each database potentially containing many schemas.
Ah, I should have refreshed the page, I see Daniel answered it much more eloquently than I did.

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.