PostgreSQL Flashcards

1
Q

Start psql in “explain backslash” command mode

A

psql -E

Good for learning about inner workings of builtin postgres dbs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List databases

A

\l

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List tables

A

\dt
\dt pg_*
\dt+ information_schema.* (also shows size, description)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Connect to a database

A

\c db_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List columns in a table

A

\d completions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Arguments to \d (list tables?)

A
n = schemas
t = tables
v = views
s = sequences
i = indexes
E = foreign tables
C = type casts
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Help with psql vs help with SQL

A

psql: \?

SQL: \h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Responsive formatting

A

\x auto

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

List users and their roles

A

\du
\du+

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a command in your editor?

A

\e

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What 4 things do you need to connect to a DB?

A
target db (-d), hostname (-h), port (-p), user (-U)
env variables: PGDATABASE, PGHOST, PGPORT and/or PGUSER
~/.pgpass for passwords
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Connect to a db with a “conninfo” string or a URI.

A

psql postgres://user@host:port/db
\conninfo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Set a variable and use command interpolation

A

\set foo bar
\echo :foo
SELECT * FROM :foo;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Export query output to file

A

COPY TO. You can also use COPY FROM to import data from a file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

List all tables, including system catalogs

A

\dtv *.*

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Command history file

A

~/.psql_history, along with the rest of the history files.

17
Q
A
18
Q

Show the search_path variable?

A

show search_path;

19
Q

Check which schema a command is actually checking…

A

explain verbose select * from …;

show search_path;

20
Q

What data type should you use for strings?

A

TEXT. More versatile than varchar(n) and char(n), same performance.

21
Q

What’s wrong with this command?

CREATE TABLE justjsonb (id integer, doc JSONB)

A

No semicolon. You should see ‘CREATE TABLE’ when the command executes successfully.