-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostgreSQL_Notes.txt
77 lines (58 loc) · 2.9 KB
/
PostgreSQL_Notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---------------------------
Redshift and Postgres Notes
---------------------------
## Install
$ brew install pgcli
## Test
$ pgcli --version
## Usage
usage: "pgcli postgresql://[user[:password]@][netloc][:port][/dbname]"
### Connect Directly
pgcli postgresql://USER:PASSWORD@CLUSTER_NAME.UID.REGION.redshift.amazonaws.com:PORT/DB"
#### For example
pgcli postgresql://admin:[email protected]:5439/dev
pgcli postgresql://[email protected]:5439/prod
### Connect via SSH Tunnel
- $ ssh [-A] -L LOCAL_PORT:REMOTE_HOST:REMOTE_PORT USER@BASTIAN_HOST # SSH tunnel
- $ pgcli -h HOST -p PORT -u USER -d DB
#### For example
- $ ssh -L 5439:prd.56fjg39aigh8.us-east-1.redshift.amazonaws.com:5439 raco@bastion # SSH tunnel
- $ pgcli -h localhost -p 5439 -u admin -d dev
## User Administration
[Users (AWS Redshift Docs)](https://docs.aws.amazon.com/redshift/latest/dg/r_Users.html)
## Commands
pgcli> SELECT * FROM pg_database
pgcli> \c <database> -- connect to DB
pgcli> \i /path/to/script.sql -- execute a script
pgcli> SELECT * FROM pg_user
pgcli> SELECT * FROM pg_user_info
pgcli> ALTER USER <user> CREATEUSER;
pgcli> CREATE USER <user> WITH PASSWORD '<password>'
pgcli> GRANT USAGE ON SCHEMA <schema> TO <user>
pgcli> GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA <schema> TO <user>
pgcli> GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA <schema> TO <user>;
pgcli> SELECT * FROM pg_namespace; -- list schemas
pgcli> SELECT distinct(tablename) FROM pg_table_def WHERE schemaname = '<schema>'; -- list tables
pgcli> SELECT version(); -- get the version number for the Amazon Redshift cluster
pgcli> SELECT * FROM SVV_EXTERNAL_SCHEMAS;
pgcli> SELECT schemaname,tablename,location FROM SVV_EXTERNAL_TABLES;
pgcli> SELECT databasename,location,esoptions FROM SVV_EXTERNAL_DATABASES;
pgcli> CREATE EXTERNAL SCHEMA <schema_name> FROM DATA CATALOG DATABASE '<db_name>' IAM_ROLE '<iam_role>' CREATE EXTERNAL DATABASE IF NOT EXISTS;
pgcli> DROP SCHEMA IF EXISTS <schema_name> DROP EXTERNAL DATABASE;
pgcli> DROP TABLE IF EXISTS <schema_name.table_name>;
pgcli> CREATE EXTERNAL TABLE <schema_name>.<table_name>
(id integer, date smallint, price decimal(8,2), time timestamp)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE
LOCATION 's3://bucket/key/'
TABLE PROPERTIES ('numRows'='NNNN');
------ connection stuff (begin) ------------
# set up a SSH Tunnel
$ ssh [-A] -L LOCAL_PORT:REMOTE_HOST:REMOTE_PORT USER@BASTIAN_HOST
# PostgreSQL - connect via URL
$ pgcli postgresql://USER:PASSWORD@HOST:PORT/DB
# PostgreSQL - connect via CL Options
$ pgcli -h HOST -p PORT -U USER -d DB
------ connection stuff (end) ------------
tags: postgresql, pgcli, postgres, redshift