Advertisement
Guest User

Untitled

a guest
Jul 11th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. -- NewDBScript.sql
  2. -- Assign variables:
  3.  
  4. set dbowner 'xxxxxxxx'
  5. set dbname 'xxxxxxxx'
  6. set dbuser 'xxxxxxxx'
  7.  
  8. -- Create the users:
  9. CREATE USER :dbowner;
  10. password :dbowner
  11.  
  12. CREATE USER :dbuser;
  13. password :dbuser
  14.  
  15. -- Create the database:
  16. CREATE DATABASE :dbname WITH OWNER :dbowner ENCODING 'UTF8' CONNECTION LIMIT -1;
  17.  
  18. -- Connect to database using postgres
  19. c :dbname postgres
  20.  
  21.  
  22. -- Create the application schema & drop the public schema
  23. CREATE SCHEMA IF NOT EXISTS :dbuser AUTHORIZATION :dbowner;
  24. DROP SCHEMA IF EXISTS public CASCADE;
  25.  
  26. -- Set the schema for the database to be the hero account schema
  27. alter database :dbname set search_path=:dbuser;
  28. show search_path;
  29.  
  30.  
  31. -- Connect to the database using database owner
  32. c :dbname :dbowner
  33. show search_path;
  34.  
  35. ...
  36.  
  37. -- After all the objects were created, I granted the following permissions to the application user:
  38.  
  39. GRANT usage on SCHEMA :dbuser to :dbuser;
  40. GRANT select, insert, update, delete on ALL TABLES IN SCHEMA :dbuser to :dbuser;
  41. GRANT usage on ALL SEQUENCES IN SCHEMA :dbuser to :dbuser;
  42.  
  43. %% part of init/1
  44. %% Open a database connection to PostgreSQL
  45. pgsql_connect(Server, Port, DB, Username, Password) ->
  46. case pgsql:connect([{host, Server},
  47. {database, DB},
  48. {user, Username},
  49. {password, Password},
  50. {port, Port},
  51. {as_binary, true}]) of
  52. {ok, Ref} ->
  53. pgsql:squery(Ref, [<<"alter database ">>, DB, <<" set ">>,
  54. <<"standard_conforming_strings='off';">>]),
  55. pgsql:squery(Ref, [<<"set standard_conforming_strings to
  56. 'off';">>]),
  57. {ok, Ref};
  58. Err ->
  59. Err
  60. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement