{"id":16987,"date":"2024-05-11T11:22:34","date_gmt":"2024-05-11T03:22:34","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?page_id=16987"},"modified":"2026-08-25T14:44:30","modified_gmt":"2026-08-25T06:44:30","slug":"postgresql","status":"publish","type":"page","link":"https:\/\/blog.ywdevops.cn\/index.php\/postgresql\/","title":{"rendered":"postgresql\u5e38\u7528\u547d\u4ee4"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\\du \u6216\u8005 \\dg                        #\u5217\u51fa\u6570\u636e\u5e93\u4e2d\u5168\u90e8\u7528\u6237\u6216\u89d2\u8272\n\\db                                 #\u663e\u793a\u6240\u6709\u7684\u8868\u7a7a\u95f4\n\\dn                                 #\u5217\u51fa\u6240\u6709\u7684schema\n\\timing on                          #\u5f00\u542f\u65f6\u95f4\uff0c\u540e\u7eed\u6267\u884csql\u8bed\u53e5\u5373\u53ef\u663e\u793a\u6267\u884c\u65f6\u95f4\n\\df                                 #\u663e\u793a\u51fd\u6570\n\\dv                                 #\u663e\u793a\u89c6\u56fe\n\\ds                                 #\u663e\u793a\u5e8f\u5217\n\\di                                 #\u663e\u793a\u7d22\u5f15\n\\dt                                 #\u663e\u793a\u5339\u914d\u7684\u8868 \n\\d + \u8868\u540d                           #\u67e5\u770b\u8868\u7ed3\u6784         \n\\h                                  #\u663e\u793a\u5e2e\u52a9\u4fe1\u606f\n\\h create table                     #\u663e\u793a\u521b\u5efa\u8868\u8bed\u6cd5\n\\l                                  #\u67e5\u770b\u5305\u542b\u54ea\u4e9b\u6570\u636e\u5e93(L\u7684\u5c0f\u5199)\n\\c db_name                          #\u5207\u6362\u5230db_name\u6570\u636e\u5e93\n\\dp \u6216 \\z                           #\u663e\u793a\u8868\u7684\u6743\u9650\u5206\u914d\u60c5\u51b5\n\\pset border 2                      #\u8f93\u51fa\u5185\u5bb9\u5185\u5916\u6709\u8fb9\u6846\uff0c\u6267\u884c\u6b64\u547d\u4ee4\u540e\u6267\u884cselect\u5373\u53ef\u770b\u5230\u6548\u679c\n\\pset border 1                      #\u8f93\u51fa\u5185\u5bb9\u53ea\u6709\u5185\u8868\u6846\n\\pset border 0                      #\u8f93\u51fa\u5185\u5bb9\u65e0\u8fb9\u6846\n\\x                                  #\u628a\u6309\u884c\u5c55\u793a\u7684\u6570\u636e\u53d8\u6210\u6309\u5217\u5c55\u793a\uff0c\u7c7b\u4f3c\u4e0eMySQL\u4e2d\u7684\\G\n\\i \/root\/a.sql;                     #\u6267\u884croot\u8def\u5f84\u4e0b\u7684sql\u6587\u4ef6\npsql -U test_user -d test_db -f \/root\/a.sql #\u7ec8\u7aef\u547d\u4ee4\u884c\u6267\u884csql\u6587\u4ef6\n\\echo hello world                   #\u8f93\u51fa\u4fe1\u606f,\u53ef\u5199\u5728sql\u811a\u672c\u4e2d\n\\?                                  #\u66f4\u591a\u5176\u4ed6\u547d\u4ee4\u67e5\u770b\n\\d + \u8fde\u7eed\u6309\u4e24\u6b21tab\u952e\u53ef\u5f39\u51fa\u7528\u6cd5\ncreate database dbname              #\u521b\u5efa\u6570\u636e\u5e93\ncreate user username with password ''  #\u521b\u5efa\u7528\u6237\nalter user postgres with password 'Abcd1234#'; #\u7ed9postgres\u7528\u6237\u8bbe\u7f6e\u5bc6\u7801\nCREATE TABLE user (\n    id SERIAL PRIMARY KEY,\n    email VARCHAR(255) NOT NULL UNIQUE,\n    password_hash TEXT NOT NULL,\n    role VARCHAR(32) NOT NULL DEFAULT 'operator',\n    email_verified BOOLEAN NOT NULL DEFAULT FALSE,\n    verify_token VARCHAR(64)\n);                                  #\u521b\u5efa\u8868\nCOMMENT ON TABLE user IS '\u7528\u6237\u8868';  #\u7ed9\u8868\u6dfb\u52a0\u6ce8\u91ca\nselect obj_description('user'::regclass); #\u67e5\u770b\u8868\u7684\u6ce8\u91ca\u5185\u5bb9\nGRANT ALL PRIVILEGES ON DATABASE dbname TO username; #\u6388\u6743\nalter user postgres with password 'Abcd1234#';  #\u91cd\u7f6epostgres\u5bc6\u7801\nALTER TABLE tablename DROP COLUMN cloumName; #\u5220\u9664\u5355\u4e2a\u5217\nALTER TABLE guacamole_connection\nDROP COLUMN col1,\nDROP COLUMN col2,\nDROP COLUMN col3;               #\u5220\u9664\u591a\u4e2a\u5217\n# \u5bfc\u51fa\u5b8c\u6574\u6570\u636e\uff1a\u683c\u5f0f\uff1apg_dump -U \u7528\u6237\u540d -d \u6570\u636e\u5e93\u540d -h \u4e3b\u673a -p \u7aef\u53e3 -f \u5bfc\u51fa\u6587\u4ef6.sql\npg_dump -U username -d dbname -h localhost -p 5432 -f full_backup.sql\n\n# \u5bfc\u5165\u6570\u636e\u5230\u6570\u636e\u5e93\u4e2d,\u6700\u597d\u4f7f\u7528postgres\u7528\u6237\uff0c\u5426\u5219\u4f1a\u63d0\u793a\u6743\u9650\u4e0d\u8db3\npsql -U postgres -d dbname -h localhost -p 5432 -f full_backup.sql\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u65b0\u5efa\u4e00\u4e2a\u5e93\uff0c\u5982\u679c\u8fd8\u60f3\u7528\u67d0\u4e2a\u666e\u901a\u7528\u6237\u8fde\u63a5\u5e76\u8ba9\u7a0b\u5e8f\u81ea\u52a8\u5efa\u8868\uff0c\u5c31\u9700\u8981\u7ed9\u8fd9\u4e2a\u5e93\u91cc\u7684 <code>public<\/code> schema \u6388\u6743\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u5b9a\u4e49\u53d8\u91cf\n\\set username 'abcd'\n\\set passwd  '123456'\n\\set dbname 'dbname'\n\n-- \u521b\u5efa\u53ef\u767b\u5f55\u7528\u6237\uff08\u91cd\u70b9\u4fee\u6b63\u5bc6\u7801\u8bed\u6cd5 :'passwd'\uff09\nCREATE USER :username WITH PASSWORD :'passwd' LOGIN;\n\n-- \u521b\u5efa\u6570\u636e\u5e93\uff0c\u76f4\u63a5\u5f52\u5c5e\u8be5\u7528\u6237\nCREATE DATABASE :dbname OWNER :username;\n\n-- \u5207\u5165\u6570\u636e\u5e93\n\\c :dbname\n\n-- schema\u6743\u9650\nGRANT USAGE, CREATE ON SCHEMA public TO :username;\n\n-- \u73b0\u6709\u6570\u636e\u8868\u3001\u5e8f\u5217\u6743\u9650\nGRANT ALL ON ALL TABLES IN SCHEMA public TO :username;\nGRANT ALL ON ALL SEQUENCES IN SCHEMA public TO :username;\n\n-- \u4e4b\u540e\u65b0\u5efa\u8868\u3001\u5e8f\u5217\u81ea\u52a8\u8d4b\u4e88\u6743\u9650\nALTER DEFAULT PRIVILEGES IN SCHEMA public\nGRANT ALL ON TABLES TO :username;\nALTER DEFAULT PRIVILEGES IN SCHEMA public\nGRANT ALL ON SEQUENCES TO :username;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u5c06\u5185\u5bb9\u5168\u90e8\u653e\u5728a.sql\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u901a\u8fc7postgres\u7528\u6237\u5bfc\u5165\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>su - postgres\npsql -U postgres -h localhost -p 5432 -f \/tmp\/a.sql  #\u9700\u8981\u5148\u7ed9postgres\u8bbe\u7f6e\u5bc6\u7801\uff0c\u53c2\u8003\u4e0a\u9762<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u65b0\u5efa\u4e00\u4e2a\u5e93\uff0c\u5982\u679c\u8fd8\u60f3\u7528\u67d0\u4e2a\u666e\u901a\u7528\u6237\u8fde\u63a5\u5e76\u8ba9\u7a0b\u5e8f\u81ea\u52a8\u5efa\u8868\uff0c\u5c31\u9700\u8981\u7ed9\u8fd9\u4e2a\u5e93\u91cc\u7684 public schema \u6388\u6743\uff0c [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-16987","page","type-page","status-publish","hentry"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/pages\/16987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=16987"}],"version-history":[{"count":35,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/pages\/16987\/revisions"}],"predecessor-version":[{"id":20900,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/pages\/16987\/revisions\/20900"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=16987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}