Home > Software engineering >  deleting tables from postgresql without raising cross-database references are not implemented: using
deleting tables from postgresql without raising cross-database references are not implemented: using

Time:10-05

I am trying to drop a table from a database. As long as name_Table is a structured as

schema.table

it all works nicely. However, I do have one table in public schema. When I try to delete it as:

public.subname.table

I get this answer:

cross-database references are not implemented: "public.subname.table"

How to drop public.subname.table?

        print('Connecting to the PostgreSQL database...')
        postgresConnection = psycopg2.connect(
                    host=XXXXXX,
                    port=YYYYYYYYY,
                    database="mydb",
                    user=os.environ['user'],
                    password=os.environ['pwd'])

     
        cursor                = postgresConnection.cursor()
        dropTableStmt   = "drop TABLE %s;"%name_Table;

        # Create a table in PostgreSQL database
        print(dropTableStmt)
        cursor.execute(dropTableStmt)
        postgresConnection.commit()
        cursor.close();
        print('Database cursor closed.')
        postgresConnection.close()
        print('Database connection closed.')

CodePudding user response:

public.subname.table runs afoul of Identifier rules in that '.' is not a valid character. The way around that is to double quote the identifier e.g. "public.subname.table" or use the function quote_ident like quote_ident(public.subname.table). In your case drop TABLE quote_ident(%s).

  • Related