Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Using Databases in Python Meet Peewee Modeling

Why does the script throw a fatal OperationalError when I try to create the database in PostgreSQL?

When attempting to create the database with my local instance of PostgreSQL, I had to make a few changes to the db variable (since PostgreSQL doesn't like being passed .db files as database names):

db = PostgresqlDatabase("students",
                        host="host",
                        port="port",
                        user="username",
                        password="password")

Even after doing this, I am greeted with the following error:

peewee.OperationalError: FATAL:  database "students" does not exist

How do I make the script create the database?

3 Answers

Unfortunately, PostgreSQL will not allow you to create the database. Instead, you will need to create the database ahead of time.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Yeah, I think SQLite is the only database engine that doesn't require the database to exist beforehand.

Randolph Judy
PLUS
Randolph Judy
Courses Plus Student 28,198 Points

I had the same problem with MySQL and creating the Students Schema before running the script resolved this error. My code was the same except for using 'MySQLDatabase()'. I verified the table creation in the MySQL Workbench.