2 # Script for adding and dropping Kamailio Postgres tables
5 # 2006-05-16 added ability to specify MD5 from a configuration file
6 # FreeBSD does not have the md5sum function (norm)
7 # 2006-07-14 Corrected syntax from MySQL to Postgres (norm)
8 # moved INDEX creation out of CREATE table statement into
9 # CREATE INDEX (usr_preferences, trusted)
10 # auto_increment isn't valid in Postgres, replaced with
11 # local AUTO_INCREMENT
12 # datetime isn't valid in Postgres, replaced with local DATETIME
13 # split GRANTs for SERWeb tables so that it is only executed
14 # if SERWeb tables are created
15 # added GRANTs for re_grp table
16 # added CREATE pdt table (from PDT module)
17 # corrected comments to indicate Postgres as opposed to MySQL
18 # made last_modified/created stamps consistent to now() using
20 # 2006-10-19 Added address table (bogdan)
21 # 2006-10-27 subscriber table cleanup; some columns are created only if
22 # serweb is installed (bogdan)
23 # 2007-01-26 added seperate installation routine for presence related tables
24 # and fix permissions for the SERIAL sequences.
25 # 2007-05-21 Move SQL database definitions out of this script (henning)
26 # 2007-05-31 Move common definitions to kamdbctl.base file (henningw)
28 # 2007-06-11 Use a common control tool for database tasks, like the kamctl
30 # path to the database schemas
31 DATA_DIR="/usr/local/share/kamailio"
32 if [ -d "$DATA_DIR/postgres" ] ; then
33 DB_SCHEMA="$DATA_DIR/postgres"
35 DB_SCHEMA="./postgres"
38 #################################################################
40 #################################################################
42 # full privileges Postgres user
43 if [ -z "$DBROOTUSER" ]; then
45 if [ ! -r ~/.pgpass ]; then
46 merr "~/.pgpass does not exist"
47 merr "create this file and add proper credentials for user postgres"
48 merr "Note: you need at least postgresql>= 7.3"
49 merr "Hint: .pgpass hostname must match DBHOST"
54 CMD="psql -q -h $DBHOST -U $DBROOTUSER "
55 DUMP_CMD="pg_dump -h $DBHOST -U $DBROOTUSER -c"
56 #################################################################
59 # execute sql command with optional db name
62 if [ $# -gt 1 ] ; then
76 kamailio_drop() # pars: <database name>
78 if [ $# -ne 1 ] ; then
79 merr "kamailio_drop function takes two params"
83 sql_query "template1" "drop database \"$1\";"
84 if [ $? -ne 0 ] ; then
85 merr "Dropping database $1 failed!"
89 # postgresql users are not dropped automatically
90 sql_query "template1" "drop user \"$DBRWUSER\"; drop user \"$DBROUSER\";"
92 if [ $? -ne 0 ] ; then
93 mwarn "Could not drop $DBRWUSER or $DBROUSER users, try to continue.."
95 minfo "Database user deleted"
98 minfo "Database $1 dropped"
102 kamailio_create () # pars: <database name>
104 if [ $# -ne 1 ] ; then
105 merr "kamailio_create function takes one param"
109 minfo "creating database $1 ..."
111 sql_query "template1" "create database \"$1\";"
112 if [ $? -ne 0 ] ; then
113 merr "Creating database failed!"
117 sql_query "$1" "CREATE FUNCTION "concat" (text,text) RETURNS text AS 'SELECT \$1 || \$2;' LANGUAGE 'sql';
118 CREATE FUNCTION "rand" () RETURNS double precision AS 'SELECT random();' LANGUAGE 'sql';"
119 # emulate mysql proprietary functions used by the lcr module in postgresql
121 if [ $? -ne 0 ] ; then
122 merr "Creating mysql emulation functions failed!"
126 for TABLE in $STANDARD_MODULES; do
127 mdbg "Creating core table: $TABLE"
128 sql_query "$1" < $DB_SCHEMA/$TABLE-create.sql
129 if [ $? -ne 0 ] ; then
130 merr "Creating core tables failed!"
135 sql_query "$1" "CREATE USER $DBRWUSER WITH PASSWORD '$DBRWPW';
136 CREATE USER $DBROUSER WITH PASSWORD '$DBROPW';"
137 if [ $? -ne 0 ] ; then
138 mwarn "Create user in database failed, perhaps they allready exist? Try to continue.."
141 for TABLE in $STANDARD_TABLES; do
142 sql_query "$1" "GRANT ALL PRIVILEGES ON TABLE $TABLE TO $DBRWUSER;"
143 sql_query "$1" "GRANT SELECT ON TABLE $TABLE TO $DBROUSER;"
144 if [ $TABLE != "version" ] ; then
145 sql_query "$1" "GRANT ALL PRIVILEGES ON TABLE "$TABLE"_id_seq TO $DBRWUSER;"
146 sql_query "$1" "GRANT SELECT ON TABLE "$TABLE"_id_seq TO $DBROUSER;"
149 if [ $? -ne 0 ] ; then
150 merr "Grant privileges to standard tables failed!"
155 if [ -e $DB_SCHEMA/extensions-create.sql ]
157 minfo "Creating custom extensions tables"
158 sql_query $1 < $DB_SCHEMA/extensions-create.sql
159 if [ $? -ne 0 ] ; then
160 merr "Creating custom extensions tables failed!"
165 minfo "Core Kamailio tables succesfully created."
167 get_answer $INSTALL_PRESENCE_TABLES "Install presence related tables? (y/n): "
168 if [ "$ANSWER" = "y" ]; then
172 get_answer $INSTALL_EXTRA_TABLES "Install tables for $EXTRA_MODULES? (y/n): "
173 if [ "$ANSWER" = "y" ]; then
179 presence_create () # pars: <database name>
181 if [ $# -ne 1 ] ; then
182 merr "presence_create function takes one param"
186 minfo "creating presence tables into $1 ..."
188 sql_query "$1" < $DB_SCHEMA/presence-create.sql
190 if [ $? -ne 0 ] ; then
191 merr "Failed to create presence tables!"
195 sql_query "$1" < $DB_SCHEMA/rls-create.sql
197 if [ $? -ne 0 ] ; then
198 merr "Failed to create rls-presence tables!"
202 for TABLE in $PRESENCE_TABLES; do
203 sql_query "$1" "GRANT ALL PRIVILEGES ON TABLE $TABLE TO $DBRWUSER;"
204 sql_query "$1" "GRANT SELECT ON TABLE $TABLE TO $DBROUSER;"
205 sql_query "$1" "GRANT ALL PRIVILEGES ON TABLE "$TABLE"_id_seq TO $DBRWUSER;"
206 sql_query "$1" "GRANT SELECT ON TABLE "$TABLE"_id_seq TO $DBROUSER;"
207 if [ $? -ne 0 ] ; then
208 merr "Grant privileges to presence tables failed!"
213 minfo "Presence tables succesfully created."
214 } # end presence_create
217 extra_create () # pars: <database name>
219 if [ $# -ne 1 ] ; then
220 merr "extra_create function takes one param"
224 minfo "creating extra tables into $1 ..."
226 for TABLE in $EXTRA_MODULES; do
227 mdbg "Creating extra table: $TABLE"
228 sql_query "$1" < $DB_SCHEMA/$TABLE-create.sql
229 if [ $? -ne 0 ] ; then
230 merr "Creating extra tables failed!"
235 for TABLE in $EXTRA_TABLES; do
236 sql_query "$1" "GRANT ALL PRIVILEGES ON TABLE $TABLE TO $DBRWUSER;"
237 sql_query "$1" "GRANT SELECT ON TABLE $TABLE TO $DBROUSER;"
238 if [ $TABLE != "route_tree" ] ; then
239 sql_query "$1" "GRANT ALL PRIVILEGES ON TABLE "$TABLE"_id_seq TO $DBRWUSER;"
240 sql_query "$1" "GRANT SELECT ON TABLE "$TABLE"_id_seq TO $DBROUSER;"
242 if [ $? -ne 0 ] ; then
243 merr "Grant privileges to extra tables failed!"
248 minfo "Extra tables succesfully created."
252 dbuid_create () # pars: <database name>
254 if [ $# -ne 1 ] ; then
255 merr "dbuid_create function takes one param"
259 minfo "creating uid tables into $1 ..."
261 for TABLE in $DBUID_MODULES; do
262 mdbg "Creating uid table: $TABLE"
263 sql_query $1 < $DB_SCHEMA/$TABLE-create.sql
264 if [ $? -ne 0 ] ; then
265 merr "Creating uid tables failed at $TABLE!"
269 minfo "UID tables succesfully created."