blog post draft
Get up and running. Install it. I'm sure you know how. But then maybe you are used to mysql>
? Where is the database? How do I even start?
sudo -u postgres -i
- This is your system account that pg runs as. Switch to it and we'll create a sandbox database.createuser name
- Where "name" is your normal user account you use.createdb name -O name
- Where "name" is your normal user account you use.- Logout:
exit
orctrl-d
. psql
from the "name" account above. If your name is Jenny or Joe, that's what I mean.jenny@laptop $ psql
- Now we're in a play database. We can mess around. Don't be shy.
\dt
- lists tables\db
- lists tablespaces (like storage areas for databases)\l
- list databases
Most of SQL will work here. So you'll be right at home coming from mysql. But I wanted to show you some other tricks. Postgres has some cool datatypes.
jenny=# create table movies (
id serial primary key,
title varchar(255) not null,
kind varchar(40),
len interval hour to minute
);
The id column is an auto incrementing column. The len column is something pretty neat. Check this out:
jenny=# insert into movies (title, kind) values ('The Fifth Element', '2 hours 7 minutes');
INSERT 0 1
jenny=# select * from movies;
id | title | kind | len
----+-------------------+-----------+----------
1 | The Fifth Element | Adventure | 02:07:00
(1 row)
Well that's neat but what is going on here with the time? Does it just recognize "english"?
jenny=# insert into movies (title, kind,len) values (E'Jurassic World', 'Action', '124 minutes');
jenny=# insert into movies (title, kind,len) values (E'Wayne\'s World', 'Comedy', '94 minutes');
jenny=# select * from movies;
id | title | kind | len
----+-------------------+-----------+----------
1 | The Fifth Element | Adventure | 02:07:00
2 | Jurassic World | Action | 02:04:00
3 | Wayne's World | Comedy | 01:34:00
(3 rows)
Yeah it does. It will store it as a real datatype. There are lots of benefits to this. Nice.
I like Postgres, except for how it stores and handles auto-increment values. Why so difficult, and not kept within the meta data of the table itself??