Languages
[Edit]
EN

PostgreSQL - Create table if not exists

0 points
Created by:
Dirask JS Member
3460

In this article, we would like to show you how to create a table if not exists in PostgreSQL.

Quick solution:

CREATE TABLE IF NOT EXISTS "table_name" (
    "column1" DATA_TYPE,
    "column2" DATA_TYPE,
    "column3" DATA_TYPE,
    ...
);

Note:

Go to the official documentation to see available DATA_TYPES.

Practical example

In this example, we create users table with the following columns and types:

  • id - INT,
  • name - VARCHAR,
  • role - VARCHAR.┬á

Query:

CREATE TABLE IF NOT EXISTS "users" (
    id SERIAL,
    name VARCHAR(100) NOT NULL,
    role VARCHAR(15) NOT NULL,
    PRIMARY KEY (id)
);

Database:

Table columns created by SQL CREATE TABLE - HeidiSQL preview
Table columns created by SQL CREATE TABLE - HeidiSQL preview

References

PostgreSQL - problems

PostgreSQL - Create table if not exists
Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join