author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Fri, 03 Apr 2015 10:57:19 +0300 | |
changeset 1709 | f34c6bc973cb |
parent 1708 | f1924a82b246 |
child 1710 | 601c31753686 |
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 |
|
1700 | 73 |
Using psql client. |
74 |
================== |
|
75 |
||
76 |
Using password file ``~/.pgpass``:: |
|
77 |
||
78 |
# comment |
|
79 |
hostname:port:database:username:password |
|
80 |
hostname:port:*:username:password |
|
81 |
hostname:*:*:username:password |
|
82 |
||
83 |
Connect by:: |
|
84 |
||
85 |
$ psql -U $USER -h $HOST $SCHEMA |
|
86 |