Postgres: How can I fix this create query?

130 Views Asked by At

I am learning Postgres and trying a create command. But it is giving error which I am unable to understand which is making error. The command is :

CREATE TABLE Package (
    id smallint NOT NULL primary key,
    package_name varchar ( 45 ) not null,
    updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    count integer(2) default null,
    pkg_desc varchar ( 45 ) not null,
    bucket_name varchar ( 45 ) not null,
    active bit(1),
    status INT(2) default null,
    metadata JSONB,
    path varchar ( 45 ) default null,
    created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    run_status INT(5) 
)

And the error:

ERROR: syntax error at or near "("
  Position: 186
2

There are 2 best solutions below

0
On

You use integer(2) and INT(2) to create a column. You must not add the number. Integers are fixed size. Also you should decide on one way to write it Try this:

CREATE TABLE Package (
    id smallint NOT NULL primary key,
    package_name varchar ( 45 ) not null,
    updated_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    count INT default null,
    pkg_desc varchar ( 45 ) not null,
    bucket_name varchar ( 45 ) not null,
    active bit(1),
    status INT default null,
    metadata JSONB,
    path varchar ( 45 ) default null,
    created_at timestamp NULL DEFAULT CURRENT_TIMESTAMP,
    run_status INT
)
0
On

integer and int types don't accept arguments. You can't write it like integer(2), which gives syntax error.

create table Package (
  id smallint not null primary key
  , package_name varchar(45) not null
  , updated_at timestamp null default current_timestamp
  , count integer default null
  , pkg_desc varchar(45) not null
  , bucket_name varchar(45) not null
  , active bit(1)
  , status int default null
  , metadata jsonb
  , path varchar(45) default null
  , created_at timestamp null default current_timestamp
  , run_status int
)

The official document should be helpful: https://www.postgresql.org/docs/13/datatype.html