PostgreSQL Error: Relation already exists

后端 未结 8 1812
暖寄归人
暖寄归人 2020-12-05 12:37

I am trying to create a table that was dropped previously.

But when I do the CREATE TABLE A ... I am getting below error:

Rela

相关标签:
8条回答
  • 2020-12-05 13:19

    Sometimes this kind of error happens when you create tables with different database users and try to SELECT with a different user. You can grant all privileges using below query.

    GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA schema_name TO username;
    

    And also you can grant access for DML statements

    GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA schema_name TO username;
    
    0 讨论(0)
  • 2020-12-05 13:32

    You cannot create a table with a name that is identical to an existing table or view in the cluster. To modify an existing table, use ALTER TABLE (link), or to drop all data currently in the table and create an empty table with the desired schema, issue DROP TABLE before CREATE TABLE.

    It could be that the sequence you are creating is the culprit. In PostgreSQL, sequences are implemented as a table with a particular set of columns. If you already have the sequence defined, you should probably skip creating it. Unfortunately, there's no equivalent in CREATE SEQUENCE to the IF NOT EXISTS construct available in CREATE TABLE. By the looks of it, you might be creating your schema unconditionally, anyways, so it's reasonable to use

    DROP TABLE IF EXISTS csd_relationship;
    DROP SEQUENCE IF EXISTS csd_relationship_csd_relationship_id_seq;
    

    before the rest of your schema update; In case it isn't obvious, This will delete all of the data in the csd_relationship table, if there is any

    0 讨论(0)
提交回复
热议问题