Connect prisma to PostgreSQL database running in docker container

Share this video with your friends

Send Tweet
Published 2 years ago
Updated 10 months ago

In this lesson you will learn how to connect your prisma to postgres db to be able to introspect it. We will also go trough the models and touch prisma naming convention.

Dimitri Ivashchuk: [0:00] Let's go further ahead and add the connection to our database. First, ensure that your database is running. Ours is running in Docker container so we can do docker ps to see that section is running. Now, we want to fix this string, so it connects to our database.

[0:21] Our user is postgres, the default one, and our db is prisma. We don't have the password field because we specified it in our environmental variables in the docker-compose file. Let's save this and run npx prisma introspect.

[0:53] As we see here, our database has been successfully instrospected so we can go to schema.prisma and see that it has picked up the correct model and it has picked up the correct types for the users, the only table that we have in database.

[1:10] By the way, to get this nice syntax highlighting, you can download and enable the Prisma extension in VS Code. Makes it easy to work with Prisma because everything looks better and you get to organize features like linting, autocomplete and all this fancy stuff.

[1:30] There is one important note that you might want to know when using Prisma documentation and for Prisma name and conventions. When you introspect the database with prisma introspect, you will get the names of the models in line with your database.

[1:46] If your Postgres is name to a snake case, then you will get this lowercase plural form of database tables which mean it would be users and Prisma has naming convention of capitalized first letter and singular form. In this case, it would be User.

[2:07] To map it now to our database, we would need to specify that we want to map to users. That would be it, but for the purposes of this tutorial, we will continue following the model with users and then when we will evolve our schema, we will redefine every model to follow Prisma convention.