Postgres (SQL) - Update Column Type
by
Jeremy Canfield |
Updated: February 20 2024
| Postgres (SQL) articles
The psql --list command can be used to display the Postgres databases. Something like this should be returned.
~]# psql --username johndoe --dbname db001 --list
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
db001 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
db002 | john.doe | UTF8 | en_US.utf8 | en_US.utf8 |
The psql command with the \dt (describe table) command can be used to list the tables in a database. Something like this should be returned.
~]# psql --username johndoe --dbname db001 --command "\dt"
List of relations
Schema | Name | Type | Owner
--------+-------------------------------+-------+-------
public | table001 | table | johndoe
public | table002 | table | johndoe
public | table003 | table | johndo
The psql command with the \d <table name> command can be used to list the columns in a table.
psql --username johndoe --dbname mydb --command "\d mytable"
Something like this should be returned.
Table "public.mytable"
Column | Type | Collation | Nullable | Default
-----------------------+--------------------------+-----------+----------+-----------------------------------------------------
id | integer | | not null |
name | varchar(30) | | |
date_updated | date | | |
Here is the syntax to change a column type.
alter table <table name> alter column <column name> type <new column type>
For example.
alter table mytable alter column name type varchar(100)
Did you find this article helpful?
If so, consider buying me a coffee over at