Getting Started

Starting Database with Docker

1
docker run --name <docker_name>
2
-e POSTGRES_PASSWORD=<password>
3
-d -p 5432:5432 postgres:13.3
4
​
5
docker exec -it <docker_name> bash
6
​
7
psql -U postgres
Copied!

Installing PostgreSQL on windows

PostgreSQL: Windows installers

Installing PostgreSQL on MacOS

Setup and Basics : using apt

Installation
1
sudo apt-get install postgresql
Copied!
Usage commands
1
service postgresql
Copied!
Switch to default user
1
sudo su postgres
Copied!

Getting Started

Connect to a database

1
Connection options:
2
-h, --host=HOSTNAME
3
# database server host or socket directory
4
# (default: "local socket")
5
-p, --port=PORT
6
# database server port
7
# (default: "5432")
8
-U, --username=USERNAME
9
# database user name (default: "root")
10
-w, --no-password
11
# never prompt for password
12
-W, --password
13
# force password prompt (should happen automatically)
Copied!
  1. 1.
    Here port : 5432 is default and can be get from psql --help
  2. 2.
    postgres is the super user. Create another user and connect using that.
  3. 3.
    to connect to database with user :
1
\c db_name user_name
Copied!

Commands

1
-- to list all tables
2
\dt
3
​
4
-- to list all databases
5
\l
6
​
7
-- to list all table spaces
8
\db
9
​
10
-- to list all schemas
11
\dn
12
​
13
-- to list all indices
14
\di
15
​
16
-- to list all sequences
17
\ds
18
​
19
-- to list all roles
20
\dg
21
​
22
-- to list data types
23
\dT
24
​
25
-- to list all domain datatypes
26
\dD
27
​
28
-- to list all views
29
\dv
30
​
31
-- to list previous commands
32
\g
33
​
34
-- to list Command History
35
\s
36
​
37
-- to run command from file
38
\i filename
39
-- it should be inside the server
40
​
41
-- to list help
42
\h
43
\h create table
44
​
45
-- to display null
46
\pset null (null)
47
​
48
-- to make terminal better
49
\pset linestyle unicode
50
\pset border 2
51
​
52
-- to watch a command in time
53
\watch 2
54
​
55
-- to turn on timing
56
\timing
Copied!
Last modified 2mo ago