question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error: relation "session" does not exist

See original GitHub issue

After installing connect-pg-simple in my Drywall app, pg is complaining as such:

error: relation "session" does not exist

I’ve created the session table in the same database as the rest of my application uses, but I still get the above exception. I’ve also tried to specify the conString property, as such:

app.use(session({
  resave: true,
  saveUninitialized: true,
  secret: config.cryptoKey,
  store: new (require('connect-pg-simple')(session))(),
  cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 }, // 30 days
  conString: 'pg://' + config.username + ':' + config.password + '@' + config.host + '/' + config.database
}));

But it doesn’t help. Ideas?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
chill117commented, Apr 28, 2020

A note is included in the documentation that says it is necessary to create the session table yourself:

Once npm installed the module, you need to create the session table in your database. For that you can use the table.sql file provided with the module:

Though it is easy to overlook. I did at first 😃

You can either manually generate the table by executing it via psql:

psql mydatabase < node_modules/connect-pg-simple/table.sql

Or you can create the table before creating your instance of pgSession. Here’s an example using knex:

const fs = require('fs');
const path = require('path');
const session = require('express-session');

const options = {
	host: 'localhost',
	port: 5432,
	user: 'postgres',
	password: '',
	database: 'postgres',
};

const knex = require('knex')({
	client: 'pg',
	connection: options,
});

knex.schema.hasTable('session').then(exists => {
	if (exists) return;
	return new Promise((resolve, reject) => {
		const schemaFilePath = path.join(__dirname, 'node_modules', 'connect-pg-simple', 'table.sql');
		fs.readFile(schemaFilePath, (error, contents) => {
			if (error) return reject(error);
			const sql = contents.toString();
			knex.raw(sql).then(() => {
				resolve();
			}).catch(reject);
		});
	});
}).then(() => {
	// Session table ready.
	const pgSession = require('connect-pg-simple')(session);
	const sessionStore = new pgSession({
		conObject: options,
	});
	console.log(sessionStore);
});

4reactions
voxpellicommented, Nov 2, 2015

The conString should be sent into new (require('connect-pg-simple')(session))() like:

store: new pgSession({
  conString : 'pg://' + config.username + ':' + config.password + '@' + config.host + '/' + config.database
}),
Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect-pg-simple express: Failed to prune sessions
Anyways, I am getting this error and I couldn't find a solution: (Failed to prune sessions: relation "session" does not exist) P.S: I ......
Read more >
ERROR: relation "session" does not exist - PokerTracker
If the "session" table is missing then it sounds like the database is corrupted. Look at the database using pgAdmin. Do you see...
Read more >
"Net_1762 ERROR: Relation does not exist" when running a ...
When creating the table on NPS, the table name was created without using double quotes, which can cause this issue. Solution.
Read more >
Postgres : Relation does not exist error
But when I try to fire a select * query, it gave me this error: dump=> select * from Approvals; ERROR: relation "approvals"...
Read more >
Sporadic StatementInvalid: relation "sessions" does not exist
So somehow your sessions table is getting deleted, a database handle is pointed to the wrong database or schema, or something is connecting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found