default port
5432
quick reference
$ psql -U postgres
Connect
postgres=# \c
Connect
postgres=# \l
Show databases
postgres=# \h
Help
postgres=# CREATE DATABASE jerry;
Create database
postgres=# DROP DATABASE jerry;
Delete database
postgres=# SET search_path TO schema;
Use schema
$ psql -U postgres -d
Use database
postgres=# \c test
Change database
postgres=# \du
List users
postgres=# \d
List tables
postgres=# CREATE SCHEMA sausalito;
Create schema
postgres=# \dn
List schema
postgres=# drop schema SAUSALITO;
Drop schema
postgres=# SELECT * FROM sausalito.employees;
Select rows
postgres=# CREATE TABLE sausalito.employees (id INT);
Create table
postgres=# INSERT INTO sausalito.employees VALUES (1);
Insert record
postgres=# UPDATE sausalito.employees SET id = 4 WHERE id = 2;
Update table record
postgres=# DELETE FROM sausalito.employees WHERE id = 3;
Delete record
postgres=# DROP TABLE sausalito.employees;
Drop table
postgres=# \q
Quit from session
placeholder syntax
The placeholder syntax differs slightly from mysql or sqlite. In those two
database servers you use ?
as the placeholder. In postgres you use $N
notation:
_, err := m.DB.Exec("INSERT INTO ... VALUES ($1, $2, $3)", ...)
types
Postgres supports the standard sql types as well as other types of general utility and a rich set of geometric types. You may additionally create user defined types.
character types
There is no performance difference among varchar
, char
, and text
apart
from increased storage space when using the blank-padded type, and a few extra
CPU cycles to check the length when storing into a length-constrained column.
While character(n)
has performance advantages in some other database systems,
there is no such advantage in PostgreSQL; in fact character(n)
is usually the
slowest of the three because of its additional storage costs. In most situations
text
or varchar
varying should be used instead.
Although the type text
is not in the SQL standard, several other SQL database
management systems have it as well.