EN
Node.js - Postgres - how to change column datatype?
1
answers
0
points
Hey, how can I change the data type in the "salary
" column from DECIMAL
to VARCHAR
in this table:
1 answer
0
points
I found a solution:
we must use ALTER TABLE
statement as follows:
ALTER TABLE table_name
ALTER COLUMN column_name TYPE new_data_type;
in this case, it will be:
ALTER TABLE "users"
ALTER COLUMN "salary" TYPE VARCHAR
Practical example in Node.js
:
const { Client } = require('pg');
const client = new Client({
host: '127.0.0.1',
user: 'postgres',
database: 'database_name',
password: 'password',
port: 5432,
});
const changeColumnType = async () => {
const query = `ALTER TABLE "users"
ALTER COLUMN "salary" TYPE VARCHAR;`;
try {
await client.connect(); // gets connection
await client.query(query); // sends query
} catch (error) {
console.error(error.stack);
} finally {
await client.end(); // closes connection
}
};
changeColumnType();
For better understanding, I also made an article:
0 comments
Add comment