Contents /
Previous /
Next
Creating and Dropping a Table
You can create a new table by specifying the table name,
along with all column names and their types:
CREATE TABLE students (
id int, -- maybe the Imat.No.
name varchar(80)
);
You can enter this into psql with the line breaks.
psql will recognize
that the command is not terminated until the semicolon.
PostgreSQL supports the usual SQL types INT, SMALLINT, REAL, DOUBLE
PRECISION, CHAR(N), VARCHAR(N), DATE, TIME, TIMESTAMP, and INTERVAL,
as well as other types of general utility and a rich set of geometric
types (e.g., POINT not SQL standard).
You can check the fields of the table with:
workshop=# \d students
Table "students"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer |
name | character varying(80) |
Controlling DEFAULT Values
When creating a table, the keyword DEFAULT and a value can be
used next to each column type. The value
will then be used anytime the column value is not supplied in an
INSERT.
If no DEFAULT is defined, a NULL is used for the
column.
CREATE TABLE account (
name CHAR(20),
balance NUMERIC(16,2) DEFAULT 0,
active CHAR(1) DEFAULT 'Y',
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
The default for the timestamp column is a call to an internal
POSTGRESQL variable that returns the current date and time.
Removing a Table
You can remove a table with:
DROP TABLE tablename;