{"id":16,"date":"2026-06-12T20:59:17","date_gmt":"2026-06-12T12:59:17","guid":{"rendered":"https:\/\/blog-1501514697.top\/?p=16"},"modified":"2026-06-12T21:00:06","modified_gmt":"2026-06-12T13:00:06","slug":"postgresql%e5%b8%b8%e7%94%a8%e5%91%bd%e4%bb%a4%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/blog-1501514697.top\/?p=16","title":{"rendered":"PostgreSQL\u5e38\u7528\u547d\u4ee4\u8bb0\u5f55"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>1.\u6e05\u7a7aschema\u4e0b\u7684\u8868\u6570\u636e\uff1a<\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"language-sql\">DO $$\n-- \u533f\u540d\u4ee3\u7801\u5757\uff0c\u5728\u8fd9\u91cc\u53ef\u4ee5\u8fdb\u884c\u53d8\u91cf\u58f0\u660e\u3001\u5faa\u73af\u3001\u6761\u4ef6\u5224\u65ad\u548c\u52a8\u6001\u6267\u884c SQL\uff0c\u50cf\u5199Java\u4e1a\u52a1\u4ee3\u7801\u4e00\u6837\nDECLARE\n    r RECORD;\nBEGIN\n-- \u628apublic schema\u4e0b\u7684\u8868\u660e\u90fd\u67e5\u51fa\u6765\uff0c\u653e\u5230r\u53d8\u91cf\u4e2d\n    FOR r IN\n        SELECT tablename\n        FROM pg_tables\n        WHERE schemaname = 'public'\n    LOOP\n-- TRUNCATE TABLE xxx \u5feb\u901f\u6e05\u7a7a\u6574\u5f20\u8868\u7684\u6570\u636e\n-- RESTART IDENTITY\uff1a\u91cd\u7f6e\u81ea\u589e\u5e8f\u5217\n-- CASCADE\uff1a\u5904\u7406\u5916\u952e\u5173\u8054\u7684\u8868\n        EXECUTE 'TRUNCATE TABLE public.' || quote_ident(r.tablename) || ' RESTART IDENTITY CASCADE';\n    END LOOP;\nEND $$;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2.\u5907\u4efdschema\u4e0b\u7684\u6240\u6709\u8868\u7684\u7ed3\u6784\u548c\u6570\u636e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"language-sql\">pg_dump -U postgres -d \u6570\u636e\u5e93\u540d -n schema\u540d &gt; backup.sql<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3.\u6062\u590dschema\u4e0b\u7684\u6240\u6709\u8868\u7684\u7ed3\u6784\u548c\u6570\u636e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"language-sql\">psql -U postgres -d \u6570\u636e\u5e93\u540d < backup.sql<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u6e05\u7a7aschema\u4e0b\u7684\u8868\u6570\u636e\uff1a DO $$ &#8212; \u533f\u540d\u4ee3\u7801\u5757\uff0c\u5728\u8fd9\u91cc\u53ef\u4ee5\u8fdb\u884c\u53d8\u91cf\u58f0\u660e\u3001\u5faa\u73af\u3001\u6761\u4ef6\u5224\u65ad\u548c\u52a8\u6001\u6267\u884c SQL\uff0c\u50cf\u5199Ja &#8230","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog-1501514697.top\/index.php?rest_route=\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog-1501514697.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog-1501514697.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog-1501514697.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog-1501514697.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16"}],"version-history":[{"count":8,"href":"https:\/\/blog-1501514697.top\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/blog-1501514697.top\/index.php?rest_route=\/wp\/v2\/posts\/16\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/blog-1501514697.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog-1501514697.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog-1501514697.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}