Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. How to access postgres
  2. sudo -u <username> psql
  3.  
  4. Disconnect from postgres (psql)
  5. \q
  6.  
  7. List Connection Information (psql)
  8. \conninfo
  9.  
  10. Create a new User/Role
  11. sudo -u postgres createuser --interactive
  12.  
  13. List all databases (psql)
  14. \l
  15.  
  16. List all users in postgres (psql)
  17. \du
  18.  
  19. Create a Database (psql)
  20. create database <database name>
  21.  
  22. Steps to add a new user (psql)
  23.  
  24. sudo -u postgres createuser --interactive
  25. sudo -u postgres createdb <username>
  26. sudo adduser <username>
  27. Set user's password
  28. ALTER USER <username> PASSWORD 'newPassword';
  29. OR
  30. \password <username>
  31.  
  32. ACCESS PROVILEGES
  33. GRANT CONNECT ON DATABASE my_db TO my_user;
  34. GRANT USAGE ON SCHEMA public TO my_user;
  35. GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO my_user;
  36. GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO my_user;
  37.  
  38. Get the version of Postgresql (psql)
  39.  
  40. SELECT version();
  41.  
  42. Get the size of a single database (psql)
  43.  
  44. SELECT pg_database_size(current_database());
  45.  
  46. Get the size of all databases (psql)
  47.  
  48. SELECT sum(pg_database_size(datname)) from pg_database;
  49.  
  50. Get the uptime of the server (psql)
  51.  
  52. SELECT date_trunc('second', current_timestamp - pg_postmaster_start_time()) as uptime;
  53.  
  54. Get total size of a database
  55.  
  56. SELECT pg_database_size(current_database());
  57.  
  58. Get total size of all Databases
  59.  
  60. SELECT sum(pg_database_size(datname)) from pg_database;
  61.  
  62. Restart Postgres server
  63. sudo systemctl restart postgresql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement