...

/

Writing a Database Model

Writing a Database Model

Learn how to write a schema and define the functions while writing a database model.

In the chapter “Structured Query Language,” we learned how to write SQL queries as separate .sql files, and we learned about using query parameters with the psql syntax for that (:variable, :'variable', and :"identifier"). For writing our database model, the same tooling is all we need.

An important aspect of using psql is its capacity to provide immediate feedback, and we can also have that with modeling. Let’s create the database first.

Press + to interact
create database sandbox;

Writing a schema

Now, we have a place where we can try things out without disturbing the existing application code. If we need to interact with existing SQL objects, it might be better to use a schema rather than a full-blown separate database:

Press + to interact
create schema sandbox;
set search_path to sandbox;

In PostgreSQL, each database is an isolated environment. A connection string must pick a target database, and it’s not ...