Sfoglia il codice sorgente

Merge pull request #347 from tracim/fix/324/postgresql_setup_review

Damien Accorsi 7 anni fa
parent
commit
9f8709a938
2 ha cambiato i file con 24 aggiunte e 220 eliminazioni
  1. 0 185
      bin/pgtool
  2. 24 35
      doc/database.md

+ 0 - 185
bin/pgtool Vedi 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
-

+ 24 - 35
doc/database.md Vedi File

@@ -1,4 +1,5 @@
1 1
 
2
+
2 3
 # Database #
3 4
 
4 5
 *Note: This page helps you setting up a **development** environment for `Tracim` and its ORM `SQLAlchemy` with `PostgreSQL` and `MySQL`. To set up a **production** environment, changing default database name, database user name and moreover its password is mandatory.*
@@ -11,8 +12,6 @@ If you want to use `PostgreSQL` as database engine:
11 12
 
12 13
 ### Minimalist introduction to PostgreSQL ###
13 14
 
14
-If you already use/know `PostgreSQL`, you can directly go to *Test the database access*.
15
-
16 15
 #### Driver ####
17 16
 
18 17
 Tracim uses the `psycopg2` driver between the `SQLAlchemy` ORM and the `PostgreSQL` RDBMS. Run the following command to install the right version:
@@ -32,50 +31,40 @@ If you changed the file, reload `PostgreSQL`:
32 31
 
33 32
 #### Creating a user and associated database ####
34 33
 
35
-You need a database and associated user/password. Tracim comes with `pgtool`, tool that will make this step easy:
36
-
37
-    ./bin/pgtool help
38
-
39
-Run the following self explanatory commands:
34
+Create user and database:
40 35
 
41
-    sudo su postgres
42
-    ./bin/pgtool create_user tracimuser tracimpassword
43
-    ./bin/pgtool create_database tracimdb
44
-    ./bin/pgtool grant_all_privileges tracimdb tracimuser
45
-    exit
36
+    sudo --user=postgres psql \
37
+         --command="CREATE USER tracimuser WITH PASSWORD 'tracimpassword';" \
38
+         --command="CREATE DATABASE tracimdb OWNER tracimuser;"
46 39
 
47
-#### Test the database access ####
40
+Test the database access:
48 41
 
49
-So, now you have a database and an associated user/password. A good habit is to test things before to use them, that's why we want to test the database access. This is easily done with Tracim `pgtool`:
42
+    psql --username=tracimuser --password --host=localhost --dbname=tracimdb \
43
+         --command="SELECT NOW();"
50 44
 
51
-    ./bin/pgtool test_connection tracimdb tracimuser tracimpassword 127.0.0.1
52
-
53
-The result is similar to the following :
54
-
55
-    PG # CONNECT TO DATABASE
56
-    ------------------------
57
-    server:     127.0.0.1
58
-    database:   tracimdb
59
-    username:   bibi
45
+Success output:
60 46
 
61 47
                   now
62 48
     -------------------------------
63
-     2014-11-10 09:40:23.306199+01
64
-    (1 row)
49
+    2017-08-25 15:46:41.105865+02
50
+    (1 ligne)
51
+
52
+Failure output:
65 53
 
66
-In case of failure, you would get something like this:
54
+    psql: FATAL:  password authentication failed for user "tracimuser"
55
+    FATAL:  password authentication failed for user "tracimuser"
67 56
 
68
-    PG # CONNECT TO DATABASE
69
-    ------------------------
70
-    server:     127.0.0.1
71
-    database:   tracimdb
72
-    username:   bibi
57
+In this case, delete the user and database and start over:
73 58
 
74
-    psql: FATAL:  password authentication failed for user "bibi"
75
-    FATAL:  password authentication failed for user "bibi"
76
-    ERRROR
59
+    sudo --user=postgres psql \
60
+         --command="DROP USER tracimuser;" \
61
+         --command="DROP DATABASE tracimdb;"
77 62
 
78
-In this case, delete the user and database you previously created, using `pgtool`, and do it again. Do not forget to run the `grant_all_rights` command!
63
+[//]: # (The following lines are only necessary to fix permissions on an existing database:)
64
+[//]: # (    sudo --user=postgres psql \)
65
+[//]: # (         --dbname=tracimdb \)
66
+[//]: # (         --command="GRANT ALL PRIVILEGES ON DATABASE tracimdb TO tracimuser;" \)
67
+[//]: # (         --command="GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO tracimuser;")
79 68
 
80 69
 ## MySQL ##
81 70