98 | | * $ psql postgres --command="\l" # do see DB on server and parameters |
99 | | * postgres:~$ createuser -D -R -S <UNIX user> |
100 | | * postgres:~$ createdb -O <postgres user> <database name> |
101 | | * $ pg_dump -Fc --verbose --file=<file name (.data)> <database name> |
102 | | * $ pg_restore --verbose --no-owner --no-privileges --dbname=<database name> <file name (.data)> # restore the data base as a different user eg. to clone a Drupal website |
| 96 | * List of DB with parameters |
| 97 | {{{#!sh |
| 98 | $ psql postgres --command="\l" |
| 99 | }}} |
| 100 | * Create user |
| 101 | {{{#!sh |
| 102 | postgres:~$ createuser -D -R -S <UNIX user> |
| 103 | }}} |
| 104 | * Create DB |
| 105 | {{{#!sh |
| 106 | postgres:~$ createdb -O <postgres user> <database name> |
| 107 | }}} |
| 108 | * Backup |
| 109 | {{{#!sh |
| 110 | $ pg_dump -Fc --verbose --file=<file name (.data)> <database name> |
| 111 | }}} |
| 112 | * Restore the data base as a different user eg. to clone a Drupal website (bad idea to clone a drupal website) |
| 113 | {{{#!sh |
| 114 | $ pg_restore --verbose --no-owner --no-privileges --dbname=<database name> <file name (.data)> |
| 115 | }}} |
| 116 | * Drop all tables in a database |
| 117 | * keywords: drop all tables from postgres database |
| 118 | * This can be copyed into psql |
| 119 | {{{#!sql |
| 120 | DROP FUNCTION IF EXISTS remove_all(); |
| 121 | |
| 122 | CREATE FUNCTION remove_all() RETURNS void AS $$ |
| 123 | DECLARE |
| 124 | rec RECORD; |
| 125 | cmd text; |
| 126 | BEGIN |
| 127 | cmd := ''; |
| 128 | |
| 129 | FOR rec IN SELECT |
| 130 | 'DROP SEQUENCE ' || quote_ident(n.nspname) || '.' |
| 131 | || quote_ident(c.relname) || ' CASCADE;' AS name |
| 132 | FROM |
| 133 | pg_catalog.pg_class AS c |
| 134 | LEFT JOIN |
| 135 | pg_catalog.pg_namespace AS n |
| 136 | ON |
| 137 | n.oid = c.relnamespace |
| 138 | WHERE |
| 139 | relkind = 'S' AND |
| 140 | n.nspname NOT IN ('pg_catalog', 'pg_toast') AND |
| 141 | pg_catalog.pg_table_is_visible(c.oid) |
| 142 | LOOP |
| 143 | cmd := cmd || rec.name; |
| 144 | END LOOP; |
| 145 | |
| 146 | FOR rec IN SELECT |
| 147 | 'DROP TABLE ' || quote_ident(n.nspname) || '.' |
| 148 | || quote_ident(c.relname) || ' CASCADE;' AS name |
| 149 | FROM |
| 150 | pg_catalog.pg_class AS c |
| 151 | LEFT JOIN |
| 152 | pg_catalog.pg_namespace AS n |
| 153 | ON |
| 154 | n.oid = c.relnamespace WHERE relkind = 'r' AND |
| 155 | n.nspname NOT IN ('pg_catalog', 'pg_toast') AND |
| 156 | pg_catalog.pg_table_is_visible(c.oid) |
| 157 | LOOP |
| 158 | cmd := cmd || rec.name; |
| 159 | END LOOP; |
| 160 | |
| 161 | FOR rec IN SELECT |
| 162 | 'DROP FUNCTION ' || quote_ident(ns.nspname) || '.' |
| 163 | || quote_ident(proname) || '(' || oidvectortypes(proargtypes) |
| 164 | || ');' AS name |
| 165 | FROM |
| 166 | pg_proc |
| 167 | INNER JOIN |
| 168 | pg_namespace ns |
| 169 | ON |
| 170 | (pg_proc.pronamespace = ns.oid) |
| 171 | WHERE |
| 172 | ns.nspname = |
| 173 | 'public' |
| 174 | ORDER BY |
| 175 | proname |
| 176 | LOOP |
| 177 | cmd := cmd || rec.name; |
| 178 | END LOOP; |
| 179 | |
| 180 | EXECUTE cmd; |
| 181 | RETURN; |
| 182 | END; |
| 183 | $$ LANGUAGE plpgsql; |
| 184 | |
| 185 | SELECT remove_all(); |
| 186 | }}} |
| 187 | http://stackoverflow.com/questions/3327312/drop-all-tables-in-postgresql |
| 188 | * http://stackoverflow.com/questions/20017552/delete-all-table-without-droping-database-in-postgres-in-django-dbshell-in-one-c |