AI Hints
Home›PostgreSQL Tutorial›Databases, Tables and psql
PostgreSQL · Lesson 4 of 120

Databases, Schemas and Tables

How data is organised.

⏱ 2 min read▶ Video at 2:32🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

One Postgres server can hold many databases. Each database holds schemas, which are like folders. And each schema holds tables. Most small apps just use the default schema, called public.

  • Server
  • Database
  • Schema
  • Table

From server to row

A server runs many databases, like one per app. A database contains schemas. A schema contains tables, views and functions. And a table holds rows, each with the same columns.

  1. Servermany databases
  2. Databaseone per app
  3. Schemapublic by default
  4. Tablerows and columns

Create a database

Create database makes a new, empty database. The pg database catalog lists every database. We skip the built in templates. Run it. Our new shop database, next to riverside and the default postgres database.

query.sqlsql
CREATE DATABASE shop;

SELECT datname FROM pg_database
WHERE NOT datistemplate
ORDER BY datname;
Output
CREATE DATABASE
  datname
-----------
 postgres
 riverside
 shop
(3 rows)

Summary

A server holds databases, databases hold schemas, and schemas hold tables. Next, meet the database we'll use all course.

Key takeaways
  • Databases
  • Schemas
  • Tables
Next up: Meet Riverside Books — our practice shop