postfix.sql 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ------------------------------------------
  2. -- ALTERNATIVE TABLE LAYOUT FOR TUTORIAL:
  3. -- Email with Postfix, Dovecot, and MySQL
  4. -- * https://www.linode.com/docs/email/postfix/email-with-postfix-dovecot-and-mysql
  5. -- * Source: https://github.com/linode/docs/blob/master/docs/email/postfix/email-with-postfix-dovecot-and-mysql.md
  6. --
  7. -- This layout is more consistence and wont require as much maintanance
  8. -- as the one in the tutorial, e.g will the setup in the tutorial
  9. -- require
  10. -- * update on every table if the domain name changes, even though this happens rarely.
  11. -- * update on aliases if the main email changes, even though this happens rarely.
  12. --
  13. -- It also ensures that
  14. -- * the child (aliases) is deleted if a user is deleted, and not just the domain.
  15. -- No "dangling" aliases...
  16. ------------------------------------------
  17. -- Layout
  18. -- | virtual_domains |
  19. -- |++++++++++++++++++++|
  20. -- | id | name |
  21. -- | int| <domain name> |
  22. -- e.g
  23. -- | 1 | domain.tld |
  24. -- | virtual_user |
  25. -- |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
  26. -- | id | domain_id | password | username |
  27. -- | int| int | <password> | <username withouth domain> |
  28. -- e.g ------------------------------------------------------------------
  29. -- | 1 | 1 | y9w2..fudh8h9 | admin |
  30. -- | virtual_aliases |
  31. -- |++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
  32. -- | id | domain_id | user_id | alias |
  33. -- | int| int | int | <alias without domain> |
  34. -- e.g ------------------------------------------------------------------
  35. -- | 1 | 1 | 1 | adminalias |
  36. -- Create table for virtual domains
  37. CREATE TABLE `virtual_domains` (
  38. `id` int(11) NOT NULL auto_increment,
  39. `name` varchar(190) NOT NULL,
  40. PRIMARY KEY (`id`)
  41. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  42. -- Create a table for all of the email addresses and passwords:
  43. CREATE TABLE `virtual_users` (
  44. `id` int(11) NOT NULL auto_increment,
  45. `domain_id` int(11) NOT NULL,
  46. `password` varchar(106) NOT NULL,
  47. `username` varchar(64) NOT NULL,
  48. PRIMARY KEY (`id`),
  49. UNIQUE KEY `email` (`username`, `domain_id`),
  50. FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
  51. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  52. -- Create a table for the email aliases:
  53. CREATE TABLE `virtual_aliases` (
  54. `id` int(11) NOT NULL auto_increment,
  55. `domain_id` int(11) NOT NULL,
  56. `user_id` int(11) NOT NULL,
  57. `alias` varchar(100) NOT NULL,
  58. PRIMARY KEY (`id`),
  59. UNIQUE KEY `email` (`alias`, `domain_id`),
  60. FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE,
  61. FOREIGN KEY (user_id) REFERENCES virtual_users(id) ON DELETE CASCADE
  62. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  63. -- Insert hostname
  64. INSERT INTO `mailserver`.`virtual_domains` (`name`) VALUES ('domain.tld')
  65. -- Insert a user
  66. INSERT INTO `mailserver`.`virtual_users` (`domain_id`, `password`, `username`)
  67. VALUES ('1', ENCRYPT('password', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'user');
  68. -- Insert an alias for user
  69. INSERT INTO `mailserver`.`virtual_aliases` (`domain_id`, `user_id`, `alias`)
  70. VALUES ('1', '1', 'alias');