author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Tue, 24 Mar 2015 17:23:15 +0200 | |
changeset 1703 | a85cc266dbdf |
parent 1702 | d2b977ccbf4f |
child 1708 | f1924a82b246 |
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 |
|
16 |
postgres=# CREATE USER "mypguser" WITH PASSWORD 'mypguserpass'; |
|
17 |
postgres=# CREATE DATABASE "mypgdatabase" OWNER "mypguser"; |
|
18 |
postgres=# \q |
|
19 |
||
20 |
Connect as user ``mypguser`` to new database:: |
|
21 |
||
22 |
$ su - mypguser |
|
23 |
$ psql mypgdatabase |
|
24 |
||
25 |
.. |
|
26 |
||
27 |
https://wiki.debian.org/PostgreSql |
|
28 |
Debian wiki instructions. |
|
29 |
||
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
30 |
List databases, schemas and tables. |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
31 |
=================================== |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
32 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
33 |
Default database is ``postgres``. |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
34 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
35 |
To list databases:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
36 |
|
1702 | 37 |
$ psql -U pgadmin -l |
38 |
||
39 |
or:: |
|
40 |
||
41 |
=> SELECT datname FROM pg_database WHERE datistemplate = false; |
|
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
42 |
=> \l |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
43 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
44 |
To switch databases:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
45 |
|
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
46 |
=> \connect NAME |
1684 | 47 |
|
48 |
Schemas:: |
|
49 |
||
50 |
=> select schema_name from information_schema.schemata; |
|
51 |
=> select nspname from pg_catalog.pg_namespace; |
|
52 |
=> \dn * |
|
53 |
||
1701
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
54 |
To list all tables in the current database:: |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
55 |
|
1702 | 56 |
=> 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
|
57 |
=> \dt |
6163ba5907d4
To switch databases.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1700
diff
changeset
|
58 |
|
1703
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
59 |
Set default schema. |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
60 |
=================== |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
61 |
:: |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
62 |
|
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
63 |
set search_path to NAME; |
a85cc266dbdf
Set default schema.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1702
diff
changeset
|
64 |
|
1700 | 65 |
Using psql client. |
66 |
================== |
|
67 |
||
68 |
Using password file ``~/.pgpass``:: |
|
69 |
||
70 |
# comment |
|
71 |
hostname:port:database:username:password |
|
72 |
hostname:port:*:username:password |
|
73 |
hostname:*:*:username:password |
|
74 |
||
75 |
Connect by:: |
|
76 |
||
77 |
$ psql -U $USER -h $HOST $SCHEMA |
|
78 |