root / pykota / trunk / initscripts / postgresql / pykota-postgresql.sql @ 3561

Revision 3561, 7.2 kB (checked in by jerome, 11 years ago)

Changed copyright years.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1--
2-- PyKota - Print Quotas for CUPS
3--
4-- (c) 2003-2013 Jerome Alet <alet@librelogiciel.com>
5-- This program is free software: you can redistribute it and/or modify
6-- it under the terms of the GNU General Public License as published by
7-- the Free Software Foundation, either version 3 of the License, or
8-- (at your option) any later version.
9--
10-- This program is distributed in the hope that it will be useful,
11-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13-- GNU General Public License for more details.
14--
15-- You should have received a copy of the GNU General Public License
16-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
17--
18-- $Id$
19--
20--
21
22--
23-- PyKota Database creation script for PostgreSQL
24--
25-- Launch this as PostgreSQL administrator with \i
26--
27
28
29--
30-- Create the print quota database
31--
32CREATE DATABASE pykota WITH ENCODING='UTF-8';
33
34--
35-- Create the print quota database users
36-- NOTE: Change the password values to the passwords you would like.
37--
38CREATE USER pykotauser WITH UNENCRYPTED PASSWORD 'readonlypw' NOCREATEDB NOCREATEUSER;
39CREATE USER pykotaadmin WITH UNENCRYPTED PASSWORD 'readwritepw' NOCREATEDB NOCREATEUSER;
40
41--
42-- Now connect to the new database
43--
44\connect pykota
45
46--
47-- Create the users table
48--
49CREATE TABLE users(id SERIAL PRIMARY KEY NOT NULL,
50                   username TEXT UNIQUE NOT NULL,
51                   email TEXT,
52                   balance FLOAT DEFAULT 0.0,
53                   lifetimepaid FLOAT DEFAULT 0.0,
54                   limitby TEXT DEFAULT 'quota',
55                   description TEXT,
56                   overcharge FLOAT NOT NULL DEFAULT 1.0);
57
58--
59-- Create the groups table
60--
61CREATE TABLE groups(id SERIAL PRIMARY KEY NOT NULL,
62                    groupname TEXT UNIQUE NOT NULL,
63                    description TEXT,
64                    limitby TEXT DEFAULT 'quota');
65
66--
67-- Create the printers table
68--
69CREATE TABLE printers(id SERIAL PRIMARY KEY NOT NULL,
70                      printername TEXT UNIQUE NOT NULL,
71                      description TEXT,
72                      priceperpage FLOAT DEFAULT 0.0,
73                      priceperjob FLOAT DEFAULT 0.0,
74                      passthrough BOOLEAN DEFAULT FALSE,
75                      maxjobsize INT4);
76
77--
78-- Create the print quota table for users
79--
80CREATE TABLE userpquota(id SERIAL PRIMARY KEY NOT NULL,
81                        userid INT4 REFERENCES users(id),
82                        printerid INT4 REFERENCES printers(id),
83                        lifepagecounter INT4 DEFAULT 0,
84                        pagecounter INT4 DEFAULT 0,
85                        softlimit INT4,
86                        hardlimit INT4,
87                        datelimit TIMESTAMP,
88                        maxjobsize INT4,
89                        warncount INT4 DEFAULT 0);
90CREATE INDEX userpquota_u_id_ix ON userpquota (userid);
91CREATE INDEX userpquota_p_id_ix ON userpquota (printerid);
92CREATE UNIQUE INDEX userpquota_up_id_ix ON userpquota (userid, printerid);
93
94--
95-- Create the job history table
96--
97CREATE TABLE jobhistory(id SERIAL PRIMARY KEY NOT NULL,
98                        jobid TEXT,
99                        userid INT4,
100                        printerid INT4,
101                        pagecounter INT4 DEFAULT 0,
102                        jobsizebytes INT8,
103                        jobsize INT4,
104                        jobprice FLOAT,
105                        action TEXT,
106                        filename TEXT,
107                        title TEXT,
108                        copies INT4,
109                        options TEXT,
110                        hostname TEXT,
111                        md5sum TEXT,
112                        pages TEXT,
113                        billingcode TEXT,
114                        precomputedjobsize INT4,
115                        precomputedjobprice FLOAT,
116                        jobdate TIMESTAMP DEFAULT now(),
117                        CONSTRAINT checkUserPQuota FOREIGN KEY (userid, printerid) REFERENCES userpquota(userid, printerid));
118CREATE INDEX jobhistory_u_id_ix ON jobhistory (userid);
119CREATE INDEX jobhistory_p_id_ix ON jobhistory (printerid);
120CREATE INDEX jobhistory_pd_id_ix ON jobhistory (printerid, jobdate);
121CREATE INDEX jobhistory_hostname_ix ON jobhistory (hostname);
122
123--
124-- Create the print quota table for groups
125--
126CREATE TABLE grouppquota(id SERIAL PRIMARY KEY NOT NULL,
127                         groupid INT4 REFERENCES groups(id),
128                         printerid INT4 REFERENCES printers(id),
129                         softlimit INT4,
130                         hardlimit INT4,
131                         datelimit TIMESTAMP);
132CREATE INDEX grouppquota_g_id_ix ON grouppquota (groupid);
133CREATE INDEX grouppquota_p_id_ix ON grouppquota (printerid);
134CREATE UNIQUE INDEX grouppquota_up_id_ix ON grouppquota (groupid, printerid);
135
136--
137-- Create the groups/members relationship
138--
139CREATE TABLE groupsmembers(groupid INT4 REFERENCES groups(id),
140                           userid INT4 REFERENCES users(id),
141                           PRIMARY KEY (groupid, userid));
142
143--
144-- Create the printer groups relationship
145--
146CREATE TABLE printergroupsmembers(groupid INT4 REFERENCES printers(id),
147                           printerid INT4 REFERENCES printers(id),
148                           PRIMARY KEY (groupid, printerid));
149--
150-- Create the table for payments
151--
152CREATE TABLE payments (id SERIAL PRIMARY KEY NOT NULL,
153                       userid INT4 REFERENCES users(id),
154                       amount FLOAT,
155                       description TEXT,
156                       date TIMESTAMP DEFAULT now());
157CREATE INDEX payments_date_ix ON payments (date);
158
159--
160-- Create the table for coefficients wrt paper sizes and the like
161--
162CREATE TABLE coefficients (id SERIAL PRIMARY KEY NOT NULL,
163                           printerid INTEGER NOT NULL REFERENCES printers(id),
164                           label TEXT NOT NULL,
165                           coefficient FLOAT DEFAULT 1.0,
166                           CONSTRAINT coeffconstraint UNIQUE (printerid, label));
167
168--
169-- Create the table for the billing codes
170--
171CREATE TABLE billingcodes (id SERIAL PRIMARY KEY NOT NULL,
172                           billingcode TEXT UNIQUE NOT NULL,
173                           description TEXT,
174                           balance FLOAT DEFAULT 0.0,
175                           pagecounter INT4 DEFAULT 0);
176
177--
178-- Set some ACLs
179--
180REVOKE ALL ON users, groups, printers, userpquota, grouppquota, groupsmembers, printergroupsmembers, jobhistory, payments, coefficients, billingcodes FROM public;
181REVOKE ALL ON users_id_seq, groups_id_seq, printers_id_seq, userpquota_id_seq, grouppquota_id_seq, jobhistory_id_seq, payments_id_seq, coefficients_id_seq, billingcodes_id_seq FROM public;
182
183GRANT SELECT, INSERT, UPDATE, DELETE, REFERENCES ON users, groups, printers, userpquota, grouppquota, groupsmembers, printergroupsmembers, jobhistory, payments, coefficients, billingcodes TO pykotaadmin;
184GRANT SELECT, UPDATE ON users_id_seq, groups_id_seq, printers_id_seq, userpquota_id_seq, grouppquota_id_seq, jobhistory_id_seq, payments_id_seq, coefficients_id_seq, billingcodes_id_seq TO pykotaadmin;
185GRANT SELECT ON users, groups, printers, userpquota, grouppquota, groupsmembers, printergroupsmembers, jobhistory, payments, coefficients, billingcodes TO pykotauser;
Note: See TracBrowser for help on using the browser.