# Postfix + Dovecot + DomainQuota



## cr4sh (Apr 12, 2013)

Hello.

I'm searching for a tutorial/info on how to add quota for a domain in dovecot-sql. I will be grateful for any information.

Regards.


----------



## graudeejs (Apr 12, 2013)

Perhaps this can help you
http://forums.freebsd.org/showpost.php?p=64528&postcount=4


----------



## cr4sh (Apr 12, 2013)

graudeejs said:
			
		

> Perhaps this can help you
> http://forums.freebsd.org/showpost.php?p=64528&postcount=4



Thank you @graudeejs I used your tutorial, it is great  But you use only quota per user. I need quota per domain, and that what I have found with Google is not so simple and all info is outdated ;/


----------



## graudeejs (Apr 13, 2013)

Looks like this could be done with dovecot2
http://www.yalo.net/notabene/linux/dovecot/dovecot_quota.html


----------



## SemFLY (Oct 5, 2015)

Hi 

*I use*
OS - FreeBSD 10.2-RELEASE
MTA - Postfix 2.11.6,1
MDA - Dovecot - 2.2.18
Database - MySQL 5.6.26

_domain - mydomain.lan
user - user@mydomain.lan_

*What iI want ?*
quota for domain
quota for per user

*Info*
All information about mydomain and users in MySQL

*What iI do and about problem*
Config quota _(dict + mysql)_
Database create _Postfixadmin 2.3.7_


```
# grep -v '^ *\(#.*\)\?$' dovecot-sql.conf
driver = mysql
connect = host=localhost dbname=postfix user=postfix password=invision127
default_pass_scheme = CRAM-MD5
password_query = SELECT username, domain, password FROM mailbox WHERE username = '%u' and domain = '%d' and active='1'
user_query = SELECT '/var/spool/mail/%d/%u' AS home, 'maildir:/var/spool/mail/%d/%u' AS mail, 1983 AS uid, 1983 AS gid, CONCAT('*:bytes=', domain.maxquota*1048576) AS quota_rule, CONCAT('*:bytes=', mailbox.quota) AS quota2_rule FROM mailbox, domain WHERE username = '%u' AND domain.domain = '%d' AND domain.active = '1'
```

- - -

In last year, iI used version Dovecot 2.2.13 and dovecot using operator "UPDATE" for changing quota field in MySQL base
_
mysql.log in real time_
-----------------

```
Connect  postfix@localhost on postfix
Query  SELECT bytes FROM quota2 WHERE username = 'user@mydomain.lan'
Query  SELECT bytes FROM quota2 WHERE username = 'user@mydomain.lan'
Query  BEGIN
Query  UPDATE quota2 SET bytes=bytes+2570,messages=messages+1 WHERE username = 'user@mydomain.lan'
Connect  postfix@localhost on postfix
Query  BEGIN
Query  UPDATE domain SET quota=quota+2570 WHERE domain = 'mydomain.lan'
Query  BEGIN
Query  UPDATE quota2 SET bytes=bytes+1723,messages=messages+1 WHERE username = 'user@mydomain.lan'
Connect  postfix@localhost on postfix
Query  BEGIN
Query  UPDATE domain SET quota=quota+1723 WHERE domain = 'mydomain.lan'
Query  UPDATE quota2 SET messages=messages+1 WHERE username = 'mydomain.lan'
Quit
```
-----------------

What now ?
Now iI build new server and using in latest version Dovecot 2.2.19 and dovecot now using method DELETE/INSERT for update quota field in MySQL base
Why method DELETE/INSERT what bad use UPDATE operator, tell me please ?
_
mysql.log in real time_

```
Connect  postfix@localhost on postfix
Query  SELECT quota FROM domain WHERE domain = 'mydomain.lan'
Query  SELECT quota FROM domain WHERE domain = 'mydomain.lan'
Query  SELECT messages FROM quota2 WHERE username = 'mydomain.lan'
Query  BEGIN
Query  DELETE FROM domain WHERE domain = 'mydomain.lan'
Query  DELETE FROM quota2 WHERE username = 'mydomain.lan'
Query
INSERT INTO domain (quota,domain) VALUES ('8581','mydomain.lan') ON DUPLICATE KEY UPDATE quota='8581'
Quit
```

Now _dic + mysql_ doesn’t work in Dovecot 2.2.19, because first execute DELETE operator and remove information from base about mydomain.lan

May be iI must another configuration Dovecot ?
Tell me please, what about this bug and how iI can fix this ?


----------

