3π
You can use select * from pg_tables;
get get a list of tables, although you probably want to exclude where schemaname <> 'pg_catalog'
β¦
Based on another one of your recent questions, if youβre trying to just drop all your django stuff, but donβt have permission to drop the DB, can you just DROP the SCHEMA that Django has everything in?
Also on your drop, use CASCADE.
EDIT: Can you select * from information_schema.tables;
?
EDIT: Your column should be row[2]
instead of row[0]
and you need to specify which schema to look at with a WHERE schemaname = 'my_django_schema_here'
clause.
EDIT: Or just SELECT table_name from pg_tables where schemaname = 'my_django_schema_here';
and row[0]
4π
cursor.execute("""SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type != 'VIEW' AND table_name NOT LIKE 'pg_ts_%%'""")
rows = cursor.fetchall()
for row in rows:
try:
cursor.execute('drop table %s cascade ' % row[0])
print "dropping %s" % row[0]
except:
print "couldn't drop %s" % row[0]
Courtesy of http://www.siafoo.net/snippet/85
- [Django]-Removing unused lookup_field generated from router on viewset
- [Django]-How to fix "cannot unpack non-iterable NoneType object" error in django admin whilst using custom user model
2π
Documentation says that ./manage.py sqlclear
Prints the DROP TABLE SQL statements for the given app name(s).
- [Django]-Django mongodbforms exception when rendering embedded formset management_form
- [Django]-Saving to database in Django tests when using ThreadPoolExecutor
2π
I use this script to clear the tables, I put it in a script called phoenixdb.sh
because it burns the DB down and a new one rises from the ashes. I use this to prevent lots of migrations in the early dev portion of the project.
set -e
python manage.py dbshell <<EOF
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
EOF
python manage.py migrate
This wipes the tables from the db without deleting the db. Your Django user will need to own the schema though which you can setup with:
alter schema public owner to django-db-user-name;
And you might want to change the owner of the db as well
alter database django-db-name owner to django-db-user-name;
- [Django]-Type object has not attribute 'get_or_create'
- [Django]-How to use FilePond in Django project
- [Django]-How do I get access to the request object when validating a django.contrib.comments form?
- [Django]-How to assign GROUP to user from a registration form (for user) in Django?
- [Django]-How much flexibility is there in a Django for loop?
0π
\dt
is the equivalent command in postgres to list tables. Each row will contain values for (schema, Name, Type, Owner)
, so you have to use the second (row[1]
) value.
Anyway, you solution will break (in MySQL and PostgreSQL) when foreign-key constraints are involved, and if there arenβt any, you might get troubles with the sequences. So the best way is in my opinion to simply drop the whole database and call initdb again (which is also the more efficient solution).
- [Django]-Continue the execution of a loop after an exception is raised
- [Django]-Django tables 2: Hyperlinking items in a column
- [Django]-Caught ViewDoesNotExist while rendering
- [Django]-Problem rendering a Django form field with different representation