JIYIK CN >

Current Location:Home > Learning > DATABASE > PostgreSQL >

Changing column types in Postgres

Author:JIYIK Last Updated:2025/04/27 Views:

This article shows how to change a column type to another data type in Postgres.

ALTER TABLETo change the column type in Postgres, use the command

ALTER TABLE <table_name>
ALTER COLUMN <column_name> [SET DATA] TYPE <new_type>;

Use 表名, , 列名and 新类型. For example:

CREATE TABLE student(
    id SERIAL PRIMARY KEY,
    name VARCHAR NOT NULL,
    admission_date DATE NOT NULL,
    contact_no INT NOT NULL,
    description TEXT
);

Output:

postgres=# \d student
                                     Table "public.student"
     Column     |       Type        | Collation | Nullable |               Default
----------------+-------------------+-----------+----------+-------------------------------------
 id             | integer           |           | not null | nextval('student_id_seq'::regclass)
 name           | character varying |           | not null |
 admission_date | date              |           | not null |
 contact_no     | integer           |           | not null |
 description    | text              |           |          |
Indexes:
    "student_pkey" PRIMARY KEY, btree (id)
postgres=#

To change the data type of student contact number and change it to , there can be or VARCHARbetween the contact numbers .+-

So the command to change the data type of the above column is:

ALTER TABLE student
ALTER COLUMN contact_no TYPE VARCHAR;

Table Description:

postgres=# ALTER TABLE student
postgres-# ALTER COLUMN contact_no TYPE VARCHAR;
ALTER TABLE
postgres=# \d student;
                                     Table "public.student"
     Column     |       Type        | Collation | Nullable |               Default
----------------+-------------------+-----------+----------+-------------------------------------
 id             | integer           |           | not null | nextval('student_id_seq'::regclass)
 name           | character varying |           | not null |
 admission_date | date              |           | not null |
 contact_no     | character varying |           | not null |
 description    | text              |           |          |
Indexes:
    "student_pkey" PRIMARY KEY, btree (id)

postgres=#

If the table is filled with some rows and contact_noin the column you have VARCHARor non-numeric values.

If you try to contact_nochange the data type of to again int, Postgres will display an You may need to specify USING <column_name>::<data_type>error called .

Use the following SQL command to insert a row:

INSERT INTO STUDENT(name,admission_date,contact_no,description)
VALUES('John Doe','2022-01-01','1212125856 ','Lorem ipsum');

Run the statement that changes the data type:

postgres=# ALTER TABLE student
postgres-# ALTER COLUMN contact_no TYPE INT;
ERROR:  column "contact_no" cannot be cast automatically to type integer
HINT:  You might need to specify "USING contact_no::integer".
postgres=#

Therefore, you need to add this line as well.

ALTER TABLE student
ALTER COLUMN contact_no TYPE VARCHAR
USING contact_no::integer;

Now, the above SQL command will be accepted. However, VARCHARit may contain leading or trailing whitespace, so you will need to strip the whitespace.

The updated command will look like this:

ALTER TABLE student
ALTER COLUMN contact_no TYPE VARCHAR
USING (trim(contact_no)::integer);

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Killing a process ID in PostgreSQL

Publish Date:2025/04/27 Views:191 Category:PostgreSQL

Today, we will learn how to kill or stop a running query in the background when working with PostgreSQL database. This may happen if the frontend stops working but a background process is still running. In this case, you may want to kill th

How to install and deploy PostgreSQL as a Docker container

Publish Date:2025/04/27 Views:50 Category:PostgreSQL

PostgreSQL , also known as Postgres, is a leading object-relational database system. It is popular because it is highly compliant with the SQL standard and includes additional features that simplify processing complex data sets at scale. Po

Add unique constraint after creating table in PostgreSQL

Publish Date:2025/04/27 Views:63 Category:PostgreSQL

Today we will learn how to add constraints after the rows in a table have been created UNIQUE . The UNIQUE constraint guarantees that the data in a row is unique in that column. So if the column ID exists, all rows will have unique values,

Creating a Schema in PostgreSQL

Publish Date:2025/04/27 Views:196 Category:PostgreSQL

This article will discuss creating schemas in PostgreSQL using SQL queries or psql. CREATE SCHEMA Use the statement to create a pattern in SQL query To create a new schema, execute the following command. CREATE SCHEMA test_schema To view al

Changing User Password in Postgres

Publish Date:2025/04/27 Views:108 Category:PostgreSQL

In this article, we will change the user password in Postgres. Changing User Passwords in Postgres Using Windows Open from the menu or search bar SQL Shell (psql) . Connect to the default database using the default port. If you set it up wi

Terminate the PostgreSQL connection

Publish Date:2025/04/11 Views:200 Category:PostgreSQL

In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep

Single query to rename and change column type in PostgreSQL

Publish Date:2025/04/11 Views:166 Category:PostgreSQL

This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial