Postgresql: how to create table only if it does not already exist?

后端 未结 10 924
不思量自难忘°
不思量自难忘° 2021-01-31 08:04

In Postgresql, how can I do a condition to create a table only if it does not already exist?

Code example appreciated.

相关标签:
10条回答
  • 2021-01-31 08:09

    What I used to check whether or not a table exists (Java & PostgreSQL) prior to creating it. I hope this helps someone. The create table portion is not implemented here, just the check to see if a table already exists. Pass in a connection to the database and the tableName and it should return whether or not the table exists.

    public boolean SQLTableExists(Connection connection, String tableName) {
        boolean exists = false;
    
        try {
            Statement stmt = connection.createStatement();
            String sqlText = "SELECT tables.table_name FROM information_schema.tables WHERE table_name = '" + tableName + "'";    
            ResultSet rs = stmt.executeQuery(sqlText);
    
            if (rs != null) {
                while (rs.next()) {
                    if (rs.getString(1).equalsIgnoreCase(tableName)) {
                        System.out.println("Table: " + tableName + " already exists!");
                        exists = true;
                    } else { 
                        System.out.println("Table: " + tableName + " does not appear to exist.");
                        exists = false;
                    }
    
                }
            }
    
        } catch (SQLException sqlex) {
            sqlex.printStackTrace();
        }
        return exists;
    }
    
    0 讨论(0)
  • 2021-01-31 08:11

    I think to check the pg_class table perhaps help you, something like that:

    SELECT COUNT (relname) as a FROM pg_class WHERE relname = 'mytable'
    
    if a = 0 then (CREATE IT)
    

    Regards.

    0 讨论(0)
  • 2021-01-31 08:13

    The easiest answer is :

    catch{
    
    #create table here
    
    }
    

    This creates a table if not exists and produces an error if exists. And the error is caught.

    0 讨论(0)
  • 2021-01-31 08:13

    http://www.postgresql.org/docs/8.2/static/sql-droptable.html

    DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
    
    0 讨论(0)
  • 2021-01-31 08:14
    create or replace function update_the_db() returns void as
    $$
    begin
    
        if not exists(select * from information_schema.tables 
            where 
                table_catalog = CURRENT_CATALOG and table_schema = CURRENT_SCHEMA
                and table_name = 'your_table_name_here') then
    
            create table your_table_name_here
            (
                the_id int not null,
                name text
            );
    
        end if;
    
    end;
    $$
    language 'plpgsql';
    
    select update_the_db();
    drop function update_the_db();
    
    0 讨论(0)
  • 2021-01-31 08:14

    Just create the table and don't worry about whether it exists. If it doesn't exist it will be created; if it does exist the table won't be modified. You can always check the return value of your SQL query to see whether the table existed or not when you executed the create statement.

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