installing on Linux
sudo apt-get update
sudo apt-get install -y postgresql
Once you do this the postgresql server automatically starts and everytime you boot your machine it will automatically restart. This type of program is called a service
A service is simply a program that auto-starts and runs in the background. For example, a database server like postgresql or Mysql. or a web server like apache or nginx.
We can see what services we have running by typing the command
service --status-all
ok back to postgres
The first thing we are going to do is add a password to the root user whose username is postgres
sudo -u postgres psql
\password postgres
Enter a password and then quit Postgres and log backin with the new password:
\q
psql -U postgres -h localhost
This looks great!
CREATE DATABASE library;
Other commands include
\l -- list databases
\u -- use a database
DROP DATABASE library; -- deletes a database
The syntax for creating a table is
CREATE TABLE table_name
(
column_name_1 DATATYPE,
column_name_2 DATATYPE
);
All keywords and identifiers are case insensitive so
REATE TABLE books
Create TaBLE BoOkS
are equivalent. Digits are allowed in table names in Postgres but not in the SQL standard so to make your database as portable as possible do not use digits like CREATE TABLE table_1
Let’s look at some basic datatypes
type | comment |
---|---|
INTEGER | |
NUMERIC | contains decimal |
TEXT | obvious –somebody’s name – a movie title – have as many characters as you need |
CHAR(2) | exactly 2 characters there is not advantage to using this |
VARCHAR(30) | at most 30 characters. Again, no advantage |
DATE | 1990-09-11 |
With that in mind let’s create our books table
CREATE TABLE books
(
id INTEGER,
author TEXT,
title TEXT,
acquired DATE
);
Now to see what tables we have in our database and the structure of each table.
\d
\d books
DROP TABLE books;
There are two ways to reading in a file.
First create a file called library.sql in your favorite editor (mine is Sublime) with the contents:
CREATE DATABASE library; \c library
CREATE TABLE books ( id INTEGER, author TEXT, title TEXT, acquired DATE );
When you are outside of the psql program you can read in the file from the command line with -f
:
psql -U postgres -h localhost -f library.sql
When you are inside of psql you can use:
\i library.sql
Great. now we have a database table but it is empty – we don’t have any data in it. how do we add information to our table? With the insert command:
INSERT INTO books (id, author, title, acquired) VALUES
(1, 'Ann Smith', 'Murder in the Javascript Lab', '2019-03-12');
select * from books;
Which will show everything we inserted into the table.
INSERT INTO books (id, title, acquired, author) VALUES
(2, 'Fibonacci Murders', '2019-03-12', 'Ron Zacharski');
and we can leave information out. say we don’t know when something was acquired.
INSERT INTO books (title, author) VALUES ('Recursive W', 'Ann Mulkern');
INSERT INTO books VALUES (10, 'Gusty Healy', 'Death by SQL Injection', '2019-05-06');
this is considered bad practice – don’t do it
INSERT INTO books (id, author, title, acquired) VALUES
(12, 'Ann Mulkern', 'Gusty Cooper and Raspberry Pi', '2018-09-12'),
(13, 'Ann Mulkern', 'Gusty Cooper and his Electric Bike', '2018-09-12'),
(14, 'Ann Mulkern', 'Gusty Cooper and his Repelotron', '2018-09-12');
come up with something creative.
NULL
Suppose we want to add the data:
id | author | title | acquired |
---|---|---|---|
12 | Ann Mulkern | Gusty Cooper and Raspberry Pi | 2018-09-12 |
13 | Ann Mulkern | Gusty Cooper and his Electric Bike | 2018-09-12 |
14 | Ann Mulkern | Gusty Cooper and his Repelotron | ??? |
but we don’t know that last date. How do we add it in. we could always add it by itself by doing:
INSERT INTO books (id, author, title) VALUES (15, 'Dr. D', 'Romance in the OR');
but we could also add a NULL to our insert.
INSERT INTO books (id, author, title, acquired) VALUES
(12, 'Ann Mulkern', 'Gusty Cooper and Raspberry Pi', '2018-09-12'),
(13, 'Ann Mulkern', 'Gusty Cooper and his Electric Bike', '2018-09-12'),
(14, 'Ann Mulkern', 'Gusty Cooper and his Repelotron', NULL);
When we are designing our database table we might think that it doesn’t make sense to have one column null. For example, it doesn’t make sense for a book not to have a name. We can specify this buy doing:
CREATE TABLE books_revised (
id INTEGER NOT NULL,
author TEXT NOT NULL,
title TEXT NOT NULL,
acquired DATE);
Try to add something that has a null in one of those columns and see what happens!
We might be getting tired of keeping track of that id number: was the last number we inserted id 21 or 25? Once we start writing our Javascript code, we could have our program keep track but that would be the wrong way to go. Postgres itself can auto increment this number. We do this by using the keyword SERIAL.
CREATE TABLE books_new
(
id SERIAL,
author TEXT NOT NULL,
title TEXT NOT NULL,
acquired DATE
);
CREATE TABLE patrons
(
id SERIAL,
name text,
town text DEFAULT 'Mesilla'
)