author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Wed, 05 Aug 2015 23:55:34 +0300 | |
changeset 1729 | 22ffd80639c0 |
parent 1711 | 379096db1a5b |
child 1905 | fba288d59662 |
permissions | -rw-r--r-- |
1683 | 1 |
.. -*- coding: utf-8; -*- |
2 |
.. include:: HEADER.rst |
|
3 |
||
4 |
========== |
|
5 |
Postgre. |
|
6 |
========== |
|
7 |
.. contents:: |
|
8 |
||
9 |
Installing on Debian. |
|
10 |
===================== |
|
11 |
||
12 |
Install and create new user and database:: |
|
13 |
||
14 |
$ sudo apt-get install postgresql postgresql-client |
|
15 |
$ sudo su - postgres |
|
1708
f1924a82b246
Update instruction.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1703
diff
changeset
|
16 |
% psql |
1683 | 17 |
postgres=# CREATE USER "mypguser" WITH PASSWORD 'mypguserpass'; |
18 |
postgres=# CREATE DATABASE "mypgdatabase" OWNER "mypguser"; |
|
19 |
postgres=# \q |
|
20 |
||
21 |
Connect as user ``mypguser`` to new database:: |
|
22 |
||
23 |
$ su - mypguser |
|
24 |
$ psql mypgdatabase |
|
25 |
||
1709
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
26 |
In order to create local host superuser:: |
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
27 |
|
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
28 |
$ sudo su - postgres |
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
29 |
$ createuser --superuser USER |
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
30 |
$ exit |
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
31 |
$ sudo -u USER psql |
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
32 |
|
1683 | 33 |
.. |
34 |
||
35 |
https://wiki.debian.org/PostgreSql |
|
36 |
Debian wiki instructions. |
|
37 |
||
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
38 |
List databases, schemas and tables. |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
39 |
=================================== |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
40 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
41 |
Default database is ``postgres``. |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
42 |
|
1709
f34c6bc973cb
Create local host superuser.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1708
diff
changeset
|
43 |
To list databases and database locales:: |
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
44 |
|
1702 | 45 |
$ psql -U pgadmin -l |
46 |
||
47 |
or:: |
|
48 |
||
49 |
=> SELECT datname FROM pg_database WHERE datistemplate = false; |
|
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
50 |
=> \l |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
51 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
52 |
To switch databases:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
53 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
54 |
=> \connect NAME |
1684 | 55 |
|
56 |
Schemas:: |
|
57 |
||
58 |
=> select schema_name from information_schema.schemata; |
|
59 |
=> select nspname from pg_catalog.pg_namespace; |
|
60 |
=> \dn * |
|
61 |
||
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
62 |
To list all tables in the current database:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
63 |
|
1702 | 64 |
=> SELECT table_schema,table_name FROM information_schema.tables ORDER BY table_schema,table_name; |
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
65 |
=> \dt |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
66 |
|
1703
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
67 |
Set default schema. |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
68 |
=================== |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
69 |
:: |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
70 |
|
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
71 |
set search_path to NAME; |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
72 |
|
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
73 |
Database, table, index size. |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
74 |
============================ |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
75 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
76 |
Database size:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
77 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
78 |
SELECT pg_database_size('geekdb'); -- in bytes |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
79 |
SELECT pg_size_pretty(pg_database_size('dbname')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
80 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
81 |
List of databases sizes:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
82 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
83 |
\l+ |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
84 |
|
1711 | 85 |
List tables sizes:: |
86 |
||
87 |
\d+ |
|
88 |
||
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
89 |
Table total size (with indexes):: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
90 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
91 |
SELECT pg_size_pretty(pg_total_relation_size('schemaname.tablename')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
92 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
93 |
Sole table size (without indexes and other):: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
94 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
95 |
SELECT pg_size_pretty(pg_relation_size('schemaname.tablename')); |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
96 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
97 |
Largest table in the PostgreSQL database:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
98 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
99 |
SELECT relname, relpages FROM pg_class ORDER BY relpages DESC; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
100 |
|
1700 | 101 |
Using psql client. |
102 |
================== |
|
103 |
||
104 |
Using password file ``~/.pgpass``:: |
|
105 |
||
106 |
# comment |
|
107 |
hostname:port:database:username:password |
|
108 |
hostname:port:*:username:password |
|
109 |
hostname:*:*:username:password |
|
110 |
||
111 |
Connect by:: |
|
112 |
||
113 |
$ psql -U $USER -h $HOST $SCHEMA |
|
114 |
||
1710
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
115 |
How to view execution plan:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
116 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
117 |
EXPLAIN query; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
118 |
EXPLAIN ANALYZE query; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
119 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
120 |
How to redirect the output of query to a file:: |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
121 |
|
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
122 |
\o output_file |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
123 |
SELECT * FROM pg_class; |
601c31753686
Database, table, index size.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1709
diff
changeset
|
124 |