Here's a shell session captured using the Unix script command.
It demonstrates basic usage of psql, which is a PostgreSQL command
line client:
# Initially creating a database, connecting to it within psql, and
# creating a table within the database. You always connect to the
# username database the first time you connect. The syntax of psql is:
#
# psql <database name> <database username>
#
# See the man page for psql for more information.
phoenix:~
* psql
Password:
Welcome to psql, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
template1=> -- This is an SQL comment.
template1=> --
template1=> -- Notice the nice help meta-commands above.
template1=> --
template1=> create database jillz;
CREATE DATABASE
template1=> -- Note that SQL statements are terminated with
template1=> -- a semicolon. If you forget, use \g.
template1=> --
template1=> -- Connect to the database I just created.
template1=> --
template1=> \c jillz
You are now connected to database jillz.
jillz=> create table example (
jillz(> id integer,
jillz(> name char(50) );
CREATE
jillz=> --
jillz=> -- About to exit
jillz=> \q
# Once you've created your database, connect to it directly when you start
# psql:
phoenix:~
* psql jillz jillz
Password:
Welcome to psql, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
jillz=> \q
phoenix:~
*
Some useful psql meta-commands:
\i <filename> --- Read and execute a list of SQL commands
in the text file <filename>. \d <table> --- List <table>'s description. \l --- List available databases on the system. \dS --- Display list of system tables. Example queries on
two system tables:
SELECT * FROM pg_user; SELECT * FROM pg_tables;
\dp --- List permissions in current database. \dt --- List tables in current database. \? in psql for the entire list and see the man
page for psql. CREATE TABLE Course ( CrsCode CHAR(6), DeptId CHAR(4), CrsName CHAR(20), Descr CHAR(100) ); -- Use text?
Some data types in PostgreSQL: (Refer to the online documentation for details)
INTEGER --- four bytes. Also SMALLINT and
BIGINT.
REAL --- single precision. Also DOUBLE PRECISION.
SERIAL --- unique serial number. Some caveats --- see
manual. CHAR(n) --- fixed length string of n
characters. VARCHAR(n) --- variable length string of at most
n characters. TEXT --- variable length string without limit. Not SQL
standard. CREATE TABLE Course ( CrsCode CHAR(6), DeptId CHAR(4), CrsName CHAR(20), Descr CHAR(100), PRIMARY KEY (CrsCode), UNIQUE (DeptId, CrsName) );
PRIMARY KEY. Table usually indexed on this.
UNIQUE constraint for each remaining candidate key.
CREATE TABLE Course (
CrsCode CHAR(6) NOT NULL,
Limit INTEGER DEFAULT '10',
Division CHAR(5),
DeptId CHAR(4),
CrsName CHAR(20),
Descr CHAR(100),
CHECK (Limit > 0 AND Limit <= (SELECT MAX(Seats) from Room)),
CHECK (Division IN ('Upper', 'Lower')) );
ALTER TABLE to add checks
once tables are populated. CREATE TABLE Teaching ( ProfId INTEGER, CrsCode CHAR(6), Semester CHAR(6), PRIMARY KEY (CrsCode, Semester), FOREIGN KEY (CrsCode) REFERENCES Course, FOREIGN KEY (ProfID) REFERENCES Professor (Id) );
In PostgreSQL the referenced key must be a primary key!