Browse Source

Reviews PostgreSQL configuration documentation

Adrien Panay 6 years ago
parent
commit
7f656f17a0
2 changed files with 6 additions and 234 deletions
  1. 0 185
      bin/pgtool
  2. 6 49
      doc/database.md

+ 0 - 185
bin/pgtool View File

@@ -1,185 +0,0 @@
1
-#!/usr/bin/python
2
-
3
-import os
4
-import sys
5
-
6
-# print 'Number of arguments:', len(sys.argv), 'arguments.'
7
-# print 'Argument List:', str(sys.argv)
8
-
9
-actions = [
10
-    'help',
11
-    'create_database',
12
-    'create_user',
13
-    'delete_database',
14
-    'delete_user',
15
-    'grant_all_privileges',
16
-    'test_connection'
17
-]
18
-
19
-def usage():
20
-    print('')
21
-    print('USAGE: '+__file__+' ['+'|'.join(action for action in actions)+'] [parameters]')
22
-    print('')
23
-    print('    Role management')
24
-    print('')
25
-    print('        '+__file__+' create_user <user_name> <password>')
26
-    print('        '+__file__+' delete_user <user_name>')
27
-    print('')
28
-    print('    Database management')
29
-    print('')
30
-    print('        '+__file__+' create_database <database_name>')
31
-    print('        '+__file__+' delete_database <database_name> --force')
32
-    print('        '+__file__+' grant_all_privileges <database_name> <user_name>')
33
-    print('')
34
-    print('    Other')
35
-    print('')
36
-    print('        '+__file__+' test_connection <database_name> <user_name> <password> [<hostname>]')
37
-    print('')
38
-    print('')
39
-    print('NOTES:')
40
-    print('    - this tool is intended to be used as \'postgres\' system user')
41
-    print('    - default hostname is 127.0.0.1')
42
-    print('')
43
-    print('')
44
-
45
-
46
-def show_help_and_exit():
47
-    usage()
48
-    exit()
49
-
50
-def on_result_and_exit(error_code):
51
-    if error_code==0:
52
-        print('')
53
-        print('')
54
-        exit(0)
55
-    
56
-    print('ERRROR')
57
-    print('')
58
-    print('')
59
-    exit(error_code)
60
-
61
-#######
62
-
63
-
64
-if len(sys.argv)<=1:
65
-    show_help_and_exit()
66
-    
67
-if sys.argv[1] not in actions:
68
-    print('Unknown command %s'%sys.argv[1])
69
-    show_help_and_exit()
70
-    
71
-if sys.argv[1]=='help':
72
-    show_help_and_exit()
73
-
74
-########################################
75
-#
76
-# BELOW ARE STANDARD ACTIONS
77
-#
78
-########################################
79
-
80
-print('')
81
-if sys.argv[1]=='create_user':
82
-    if len(sys.argv)!=4:
83
-        show_help_and_exit()
84
-    user_name = sys.argv[2]
85
-    user_pass = sys.argv[3]
86
-    
87
-    print('PG # CREATE USER')
88
-    print('----------------')
89
-    print('name:       '+user_name)
90
-    print('password:   XXX')
91
-
92
-    sql = '"CREATE USER %s WITH PASSWORD \'%s\';"' %(user_name, user_pass)
93
-    result = os.system('echo %s|psql' % (sql))
94
-    on_result_and_exit(result)
95
-
96
-
97
-if sys.argv[1]=='delete_user':
98
-    if len(sys.argv)!=3:
99
-        show_help_and_exit()
100
-
101
-    user_name = sys.argv[2]
102
-    
103
-    print('PG # DELETE USER')
104
-    print('------------------')
105
-    print('name:       '+user_name)
106
-    print('')
107
-
108
-    sql = '"DROP ROLE %s;"' %(user_name)
109
-    result = os.system('echo %s|psql' % (sql))
110
-    on_result_and_exit(result)
111
-
112
-
113
-if sys.argv[1]=='create_database':
114
-    if len(sys.argv)!=3:
115
-        show_help_and_exit()
116
-    name = sys.argv[2]
117
-
118
-    print('PG # CREATE DATABASE')
119
-    print('--------------------')
120
-    print('name:       '+name)
121
-    print('')
122
-    
123
-    sql = '"CREATE DATABASE %s;"' %(name)
124
-    result = os.system('echo %s|psql' % (sql))
125
-    on_result_and_exit(result)
126
-
127
-if sys.argv[1]=='delete_database':
128
-    if len(sys.argv)!=4:
129
-        show_help_and_exit()
130
-    name = sys.argv[2]
131
-    force = sys.argv[3]
132
-    
133
-    print('PG # DROP DATABASE')
134
-    print('------------------')
135
-    print('name:       '+name)
136
-    print('')
137
-    
138
-    if force!='--force':
139
-        print('Add --force option in order to drop a database')
140
-        on_result_and_exit(-1)
141
-        
142
-    sql = '"DROP DATABASE %s;"' %(name)
143
-    result = os.system('echo %s|psql' % (sql))
144
-    on_result_and_exit(result)
145
-
146
-
147
-if sys.argv[1]=='grant_all_privileges':
148
-    if len(sys.argv)!=4:
149
-        show_help_and_exit()
150
-    database_name = sys.argv[2]
151
-    user_name = sys.argv[3]
152
-    
153
-    print('PG # GRANT ALL PRIVILEGES ON DATABASE')
154
-    print('-------------------------------------')
155
-    print('database:   '+database_name)
156
-    print('user:       '+user_name)
157
-    print('')
158
-    
159
-    sql = '"GRANT ALL PRIVILEGES ON DATABASE %s to %s;"' % (database_name, user_name)
160
-    result = os.system('echo %s|psql' % (sql))
161
-    on_result_and_exit(result)
162
-
163
-if sys.argv[1]=='test_connection':
164
-    if len(sys.argv)<5:
165
-        show_help_and_exit()
166
-    
167
-    hostname = '127.0.0.1'
168
-    database_name = sys.argv[2]
169
-    user_name = sys.argv[3]
170
-    password = sys.argv[4]
171
-    
172
-    if len(sys.argv)>=6:
173
-        hostname = sys.argv[5]
174
-    
175
-    print('PG # CONNECT TO DATABASE')
176
-    print('------------------------')
177
-    print('server:     '+hostname)
178
-    print('database:   '+database_name)
179
-    print('username:   '+user_name)
180
-    print('')
181
-    
182
-    result = os.system('export PGPASSWORD=%s; echo "SELECT NOW();"|psql -h %s -U %s %s' % (password, hostname, user_name, database_name))
183
-    on_result_and_exit(result)
184
-
185
-

+ 6 - 49
doc/database.md View File

@@ -12,8 +12,6 @@ If you want to use `PostgreSQL` as database engine:
12 12
 
13 13
 ### Minimalist introduction to PostgreSQL ###
14 14
 
15
-If you already use/know `PostgreSQL`, you can directly go to *Test the database access*.
16
-
17 15
 #### Driver ####
18 16
 
19 17
 Tracim uses the `psycopg2` driver between the `SQLAlchemy` ORM and the `PostgreSQL` RDBMS. Run the following command to install the right version:
@@ -33,8 +31,6 @@ If you changed the file, reload `PostgreSQL`:
33 31
 
34 32
 #### Creating a user and associated database ####
35 33
 
36
-##### With `psql`, the `PostgreSQL` interactive terminal
37
-
38 34
 Create user and database:
39 35
 
40 36
     sudo --user=postgres psql \
@@ -58,57 +54,18 @@ Failure output:
58 54
     psql: FATAL:  password authentication failed for user "tracimuser"
59 55
     FATAL:  password authentication failed for user "tracimuser"
60 56
 
57
+In this case, delete the user and database and start over:
58
+
59
+    sudo --user=postgres psql \
60
+         --command="DROP USER tracimuser;" \
61
+         --command="DROP DATABASE tracimdb;"
62
+
61 63
 [//]: # (The following lines are only necessary to fix permissions on an existing database:)
62 64
 [//]: # (    sudo --user=postgres psql \)
63 65
 [//]: # (         --dbname=tracimdb \)
64 66
 [//]: # (         --command="GRANT ALL PRIVILEGES ON DATABASE tracimdb TO tracimuser;" \)
65 67
 [//]: # (         --command="GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO tracimuser;")
66 68
 
67
-##### With `pgtool`
68
-
69
-Tracim comes with `pgtool` to ease this step:
70
-
71
-    ./bin/pgtool help
72
-
73
-Run the following self explanatory commands:
74
-
75
-    sudo su postgres
76
-    ./bin/pgtool create_user tracimuser tracimpassword
77
-    ./bin/pgtool create_database tracimdb
78
-    ./bin/pgtool grant_all_privileges tracimdb tracimuser
79
-    exit
80
-
81
-Test the database access:
82
-
83
-    ./bin/pgtool test_connection tracimdb tracimuser tracimpassword 127.0.0.1
84
-
85
-Success output:
86
-
87
-    PG # CONNECT TO DATABASE
88
-    ------------------------
89
-    server:     127.0.0.1
90
-    database:   tracimdb
91
-    username:   bibi
92
-
93
-                  now
94
-    -------------------------------
95
-     2014-11-10 09:40:23.306199+01
96
-    (1 row)
97
-
98
-Failure output:
99
-
100
-    PG # CONNECT TO DATABASE
101
-    ------------------------
102
-    server:     127.0.0.1
103
-    database:   tracimdb
104
-    username:   bibi
105
-
106
-    psql: FATAL:  password authentication failed for user "bibi"
107
-    FATAL:  password authentication failed for user "bibi"
108
-    ERRROR
109
-
110
-In this case, delete the user and database you previously created using `pgtool`, and do it again.
111
-
112 69
 ## MySQL ##
113 70
 
114 71
 Or if you want to use `MySQL` as database engine