EN
Node.js - PostgreSQL - delete row where column is empty string (blank)
0 points
In this article, we would like to show you how to delete a row where the column is an empty string in the PostgreSQL database using Node.js.

Note: at the end of this article you can find database preparation SQL queries.
xxxxxxxxxx
1
const { Client } = require('pg');
2
3
const client = new Client({
4
host: '127.0.0.1',
5
user: 'postgres',
6
database: 'database_name',
7
password: 'password',
8
port: 5432,
9
});
10
11
const deleteRows = async (text) => {
12
const query = `DELETE FROM "users" WHERE "email" = $1;`;
13
try {
14
await client.connect(); // creates connection
15
await client.query(query, [text]); // sends query
16
} catch (error) {
17
console.error(error.stack);
18
} finally {
19
await client.end(); // closes connection
20
}
21
};
22
23
deleteRows(''); // delete row where column is empty string ''
Result:

create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE "users" (
2
"id" SERIAL,
3
"name" VARCHAR(50) NOT NULL,
4
"surname" VARCHAR(50) NOT NULL,
5
"email" VARCHAR(50),
6
PRIMARY KEY ("id")
7
);
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO "users"
2
( "name", "surname", "email")
3
VALUES
4
('John', 'Stewart', 'john@email.com'),
5
('Chris', 'Brown', ''),
6
('Kate', 'Lewis',''),
7
('Ailisa', 'Gomez', 'ailisa@email.com'),
8
('Gwendolyn', 'James', ''),
9
('Simon', 'Collins', ''),
10
('Taylor', 'Martin',''),
11
('Andrew', 'Thompson', 'andrew123@email.com');