I'm building a user signup feature into an application using Node.JS, PostgreSQL for database and 'pg' for ORM.
The user provides their email, password, phone, country and the data is stored into the Postgresql database along with other metadata.
The code works fine when I test it locally on my computer.
However, when I try using it in production, I get an error that looks like below if the user submits the signup data.
{"length":102,"name":"error","severity":"ERROR","code":"42501","file":"sequence.c","line":"463","routine":"nextval_internal"}
I can't figure out if this is a postgresQL database error, or a Node.JS error.
The error files (stderr) in my production 'file manager' do not record any errors.
The Node.JS database query looks like below;
db.query('INSERT INTO traveler (name, email, phone, password, joined, uid, status, verification, photo, forgot, type, country, platform) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)',
[name, email, phone, encryptedPWD, date, uid, status, verification, imgBio, forgot, type, country, platform]
Could you please help me spot the problem?
Thanks in advance!
The Posgresql 'traveler' database table schema looks like below.
CREATE TABLE IF NOT EXISTS traveler (
id SERIAL PRIMARY KEY NOT NULL,
phone varchar(20) NOT NULL,
email varchar(255) NOT NULL,
name varchar(255) NOT NULL,
joined varchar(255) NOT NULL,
uid varchar(255) NOT NULL,
verification varchar(10) NOT NULL,
status varchar(10) NOT NULL,
photo varchar(255) NULL,
password varchar(255) NOT NULL,
type varchar(255) NULL,
country varchar(255) NULL,
forgot varchar(255) NULL,
platform varchar(255) NULL,
);
The error code 42501
corresponds to Insufficient Privilege
from PostgreSQL. From the error, it seems the user is missing USAGE
and SELECT
permission on the sequence table of the table in which you're trying to do insert.
You can fix this by giving the right permission to the user:
GRANT USAGE, SELECT ON SEQUENCE <table_id_seq> TO user;
Or you can choose to grant the USAGE
permission on all sequence tables in your current schema.
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO user;
You can read more about granting permissions here.