Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.