Connect with psql and create tables
PostgreSQL 19.0
Start the server you installed in the previous lesson. Choose the connection command for your operating system, then create a practice database and set up students, courses, and enrollments.
On this page
Connect from Windows PowerShell
Run this from PowerShell and enter the PostgreSQL password you chose during installation if prompted. If your port or role differs, replace the corresponding value.
psql -h localhost -p 5432 -U postgres -d postgres
psql- Starts the PostgreSQL terminal client.
-h localhost- Selects the host. localhost is the machine on which you run psql.
-p 5432- Selects the TCP port on that machine.
-U postgres- Logs in as the PostgreSQL role named postgres.
-d postgres- Opens the database named postgres. The role and database happen to share a name in this example.
What to expect A prompt such as postgres=# means you are inside psql. Password input may stay invisible as you type.
Connect from macOS with Postgres.app
In Terminal, use your macOS account name as the default Postgres.app role. The shell expands whoami to that account name. Replace the port if your Postgres.app server uses a different one.
psql -h localhost -p 5432 -U "$(whoami)" -d postgres
Connect from Ubuntu with the local postgres account
On a typical Ubuntu package installation this connects through the local Unix socket. sudo -u postgres runs psql as the operating-system user postgres, which can match the PostgreSQL role under peer authentication. The -d option selects the postgres database.
sudo -u postgres psql -d postgres
Use the short postgres login command
Run this in your OS terminal. It selects the postgres role. With the default client settings, psql also uses postgres as the database name and uses the default local connection and port 5432. If your local authentication rejects it, use the full command for your OS above.
psql -U postgres
Read the psql prompt
The prompt is printed by the program. Type only the command that follows it. If terminal flags typed inside psql leave you at a continuation prompt, use the recovery steps below.
postgres=# SELECT
postgres-# CURRENT_DATE;
current_date
--------------
2026-11-24
(1 row)
postgres=#- Ready for a command in the postgres database. The # normally indicates a superuser. A normal role sees postgres=> with the default prompt.
postgres-#- The statement is unfinished, often because the SQL statement is missing a final semicolon.
\r / Ctrl+C- Use the backslash r command to clear a pending statement, or press Ctrl+C if you are stuck in an open quote. Run \q to leave psql before retrying an OS terminal command.
Create a practice database
Inside psql, run CREATE DATABASE as a role with database-creation permission. Then run the psql command \c to switch databases without leaving the client. Use the practice database for the following lessons.
CREATE DATABASE learning_db;
\c learning_db
What to expect CREATE DATABASE, followed by a message that you are connected to learning_db. The prompt starts with learning_db.
If the result differs If learning_db already exists, run just \c learning_db. A local client can see “connection refused” when nothing is listening on the chosen address, and “password authentication failed” when the role or password is wrong for that installation.
Create students, courses, and enrollments
Run this in learning_db. Each enrollment links one student to one course. A student can take several courses, and a course can have several students. Create the two parent tables before creating enrollments. Run the whole example if you already have learning_db from an earlier lesson.
CREATE TABLE IF NOT EXISTS students (
id integer PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE IF NOT EXISTS courses (
id integer PRIMARY KEY,
title text NOT NULL,
fee numeric(10, 2) NOT NULL CHECK (fee >= 0)
);
CREATE TABLE IF NOT EXISTS enrollments (
student_id integer REFERENCES students(id),
course_id integer REFERENCES courses(id),
enrolled_on date NOT NULL,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students (id, name)
VALUES (1, 'Aye'), (2, 'Min'), (3, 'Su')
ON CONFLICT (id) DO NOTHING;
INSERT INTO courses (id, title, fee)
VALUES (1, 'SQL Basics', 30.00), (2, 'Web Development', 25.00),
(3, 'Computer Fundamentals', 20.00)
ON CONFLICT (id) DO NOTHING;
INSERT INTO enrollments (student_id, course_id, enrolled_on)
VALUES (1, 1, DATE '2026-01-05'), (2, 1, DATE '2026-01-06'),
(1, 2, DATE '2026-01-07')
ON CONFLICT (student_id, course_id) DO NOTHING;
students / courses- The two parent tables store student names and course details. Each table uses its own ID as the primary key.
enrollments / REFERENCES- The foreign keys require both IDs to exist. The two-column primary key prevents a student from enrolling in the same course twice.
IF NOT EXISTS / ON CONFLICT- Running the example again keeps the existing tables and skips seed rows whose keys are already present.
What to expect A fresh example has three students, three courses, and three enrollments. Aye takes two courses and Computer Fundamentals has no enrollments.