By WebNest Studio Engineering Team
PostgreSQL Tutorial
PostgreSQL Setup Concepts: Server, psql, and Databases
PostgreSQL is a client-server relational database system. A single running process, the PostgreSQL server (postgres), listens on a port (5432 by default) and manages one or more databases, handling every connection, query, and transaction. To work with it, you use a client — most commonly psql, the official interactive command-line client that ships with every PostgreSQL installation.
Before writing a single query, you need to understand the connection model: a client connects to a specific database on a specific server using a host, port, username, and database name. Everything you do afterward — creating tables, running queries, managing permissions — happens inside the context of that one database.
Why it matters
PostgreSQL is the most widely recommended open-source database for new projects, and psql is the tool you will have on every server, whatever graphical client you prefer. A handful of its backslash commands are enough to find your way around any database you are handed.
Connecting with psql
You typically connect with a command like psql -h localhost -p 5432 -U postgres -d mydb, or simply psql mydb if defaults are configured. Once connected, psql gives you an interactive prompt (mydb=#) where you can type SQL statements ending in a semicolon, or special "meta-commands" that start with a backslash and are handled by psql itself rather than sent to the server.
Essential Meta-Commands
Meta-commands are how you explore a database's structure quickly without writing SQL against system catalogs by hand.
\l— list all databases on the server.\c dbname— connect to a different database.\dt— list all tables in the current schema.\d tablename— describe a table: columns, types, indexes, constraints, and foreign keys.\dn— list schemas in the current database.\du— list roles (users/groups) and their attributes.\q— quit psql.
Databases Are Isolated
A PostgreSQL server can host many databases, but a single connection can only query one database at a time — there is no cross-database JOIN like there is cross-schema. Every fresh install includes a default postgres database (used for administrative connections) and typically a template1 database that new databases are cloned from when you run CREATE DATABASE.
Example
Connecting, creating a database, and inspecting it with meta-commands
-- From the terminal:
-- psql -U postgres
CREATE DATABASE bookstore;
\c bookstore
CREATE TABLE authors (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);
\dt
\d authors
CREATE DATABASE
You are now connected to database "bookstore" as user "postgres".
CREATE TABLE
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | authors | table | postgres
Table "public.authors"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+------------------------------------
id | integer | | not null | nextval('authors_id_seq'::regclass)
name | text | | not null |
Indexes:
"authors_pkey" PRIMARY KEY, btree (id)
Common Mistakes
- Running CREATE TABLE against the wrong database because you forgot to \c into the intended one first — psql's prompt always shows the current database name, so check it.
- Confusing a PostgreSQL "database" with a "schema" — they are different levels of isolation, and \dt only shows tables in schemas on your current search_path within the current database.
- Trying to query another database directly in a plain SQL statement — PostgreSQL requires a new connection (or an extension like dblink/postgres_fdw) to reach another database.
- Forgetting the terminating semicolon in psql, which leaves the prompt waiting on a continuation line ("mydb-#") instead of running the statement.
Exercise
This exercise uses the psql client, so it needs a PostgreSQL installation and cannot run in the playground. Write the commands, in order, to: create a database named shop; connect to it; create a table products with an integer primary key id and a text name; list the tables; describe the products table; and quit.
Expected result: the list of tables shows products, and the description shows its two columns and the primary key index.
-- 1. create the database shop
-- 2. connect to it
-- 3. create the products table
-- 4. list the tables
-- 5. describe products
-- 6. quit
Show hints
- SQL statements end with a semicolon; psql meta-commands start with a backslash and do not.
- The meta-commands are
\cto connect,\dtto list tables,\d nameto describe one and\qto quit.
Show solution
-- 1. create the database shop
CREATE DATABASE shop;
-- 2. connect to it
\c shop
-- 3. create the products table
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT
);
-- 4. list the tables
\dt
-- 5. describe products
\d products
-- 6. quit
\q
Quick quiz
1. Which psql meta-command lists the tables in the current database?
- A. \l
- B. \dt
- C. \c
- D. \q
Show answer
B. \l lists databases, \c connects to one, and \q quits.
2. On which port does PostgreSQL listen by default?
- A. 5432
- B. 3306
- C. 8080
- D. 27017
Show answer
A. 3306 is the default for MySQL and 27017 for MongoDB.
3. Can a single query join tables from two different PostgreSQL databases on the same server?
- A. Yes, always
- B. Only with SELECT *
- C. Only inside a transaction
- D. No; databases are isolated, and a connection sees only one of them
Show answer
D. Schemas inside one database are the usual way to separate groups of tables that must still be queried together.
Interview questions
What makes PostgreSQL different from other relational databases?
Show answer
It is open source with a permissive licence, follows the SQL standard closely, and has an unusually rich feature set: JSONB for document data, arrays, range types, full-text search, several index types including GIN and GiST, window functions, common table expressions and transactional DDL. It can be extended with new types, functions and extensions such as PostGIS for geographic data. Its reliability under concurrency makes it a common default for new applications.
Which psql commands do you use most?
Show answer
\l lists the databases and \c name connects to one. \dt lists tables, \d table shows a table's columns, indexes and constraints, \dn lists schemas and \du lists roles. \x switches to expanded output for wide rows, \timing shows how long each query takes, \i file runs a script, and \q quits.
Key Points to Remember
- PostgreSQL follows a client-server model; psql is the standard interactive client.
- A server hosts multiple databases, but one connection queries only one database at a time.
- \dt lists tables, \d tablename describes one table in full, and \l lists all databases.
- Meta-commands (backslash commands) are handled by psql itself, not sent to the server as SQL.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs your SQL on SQLite in your browser. Standard SQL works; a few features that exist only in PostgreSQL or MySQL do not. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering