Comments (19)
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);
});
from node-connect-pg-simple.
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
}),
from node-connect-pg-simple.
@asbjornu Just in case you haven't seen it, there's another project that is built specifically for Sequelize: https://www.npmjs.com/package/connect-session-sequelize
from node-connect-pg-simple.
As of March 23 2021, this again has struck me!
from node-connect-pg-simple.
@voxpelli Thanks, I'll try that tonight. But optimally, the conString
property shouldn't be necessary, right? I already have Sequelize configured in the application, so shouldn't connect-pg-simple
be able to use it somehow? How?
from node-connect-pg-simple.
@asbjornu Well, this module talks directly to PostgreSQL through the pg module and isn't specifically tailored to any of the ORM:s out there – so it doesn't know what configuration an ORM like Sequelize has set up for it.
What it however does to is to default to any connection string found in the DATABASE_URL
environment variable as that's how eg. Heroku by default specify their database connection strings, so as long as one follow that same pattern, one doesn't have to send in a connection string manually.
from node-connect-pg-simple.
@voxpelli Ah, I haven't seen that before, no. Thanks for mentioning it, I'll definitely check it out! :)
from node-connect-pg-simple.
@voxpelli connect-session-sequelize worked perfectly. Thanks for the awesome tip! 😄
from node-connect-pg-simple.
also running into the error relation "session" does not exist
. intuitively, it would be helpful for the library to use the same pg
connection as is defined elsewhere in the project. I'm using knex
only, not sequelize
. Any suggestions on how to fix the error?
EDIT: I will try using https://www.npmjs.com/package/connect-session-knex
from node-connect-pg-simple.
@s2t2 Did you try specifying the connection details as noted in my first comment here?
This module tries to reuse the same connection details as elsewhere as much as it can, but apart from relying on standard environment variables it can't automatically detect such details really and then you have to manually send it in by doing what suggested in that comment.
I myself use this module with Knex, there's really no need to use something Knex-specific, one just need to provide it with the same connection info as the one that is given to Knex itself.
from node-connect-pg-simple.
@s2t2 The reason one has to provide the connection details one way or the other, through a standard environment variable or manually, is because of how the connection pool works with Postgres in node. One provides the connection details and it ensures one gets a working connection from its pool of connections for those details. Here's the docs if you're curious to read more about it: https://github.com/brianc/node-postgres/wiki/pg#connectstring-connectionstring-function-callback
from node-connect-pg-simple.
Thanks for following up. I will note these instructions for future usage. The knex-specific library was what I was looking for, and is suiting my needs at this time.
from node-connect-pg-simple.
Hi, wanna understand that everything working fine in connect pg simple module ,connected to database and done with inserting in table but when i tried to run application end up with no session working up.
please make under stand what needs to be done..
from node-connect-pg-simple.
Anyone stumbling on this error in the future may want to check if Sequelize is still sneakiy pluralizing their table name from session
to sessions
(sequelize/sequelize#7307).
from node-connect-pg-simple.
(connect-pg-simple)
i have run onto this problem and i just created the table manually in the heroku database
CREATE TABLE public.session (
sid character varying NOT NULL,
sess json NOT NULL,
expire timestamp(6) without time zone NOT NULL
);
and boom! it works.
from node-connect-pg-simple.
(connect-pg-simple)
i have run onto this problem and i just created the table manually in the heroku databaseCREATE TABLE public.session (
sid character varying NOT NULL,
sess json NOT NULL,
expire timestamp(6) without time zone NOT NULL
);and boom! it works.
CREATE TABLE public.session (
sid character varying PRIMARY KEY NOT NULL,
sess json NOT NULL,
expire timestamp(6) without time zone NOT NULL
);
Would feet better! :)
from node-connect-pg-simple.
Im having the same issue, i am using sails js as a frame work and Iv tried every way to connect, pool, conObject and conString but every time it says Failed to prune sessions: relation “session” does not exist.
I have tested the connection by connecting and selecting the table names, and session is there.
const pgPool = new Pool({
connectionString: ‘<connectionString>’,
ssl: {
sslmode: ‘require’,
}
});
...
...
session: {
adapter: ‘connect-pg-simple’,
store: new pgSession({
pool: pgPool,
tableName: ‘session’
}),
secret: ‘shhhhhhh’,
cookie: {
secure: true,
maxAge: 24 * 60 * 60 * 1000, // 24 hours
},
resave: true,
saveUninitialized: true
}
any help would be great.
from node-connect-pg-simple.
@dan-ling Can you open a new issue + preferable provide a working reproduction in eg https://codesandbox.io/
from node-connect-pg-simple.
Will make a new issue now
from node-connect-pg-simple.
Related Issues (20)
- Error thrown when setting a session when connecting to Heroku-hosted PostgreSQL HOT 2
- @types/pg as dev dependency HOT 7
- Use ES6 import HOT 3
- Change `pg` to a peer dependency HOT 2
- db.bit.io issues HOT 8
- 'Failed to prune sessions: con.connect is not a function' when trying to connect PostgreSQL database with session store via Knex.js HOT 1
- error: date/time field value out of range: "1655239143" HOT 4
- Failed to prune sessions: relation "session" does not exist HOT 3
- Two sessions being saved to store HOT 1
- If at the first try to create the session table the database is down, the store fails forever HOT 3
- Getting error "Error: connect ECONNREFUSED ip_address:5432" intermittently when the RDS is restarted.
- Getting error "Error: connect ECONNREFUSED ip_address:5432" intermittently when the RDS is restarted. HOT 2
- Unexpected token in node_module index.js HOT 2
- Using "pruneSessionInterval=false" will unexpectedly disable "createTableIfMissing=true" HOT 3
- Property "replaceAll" does not exist on type "string". HOT 4
- pg-simples tries to access wrong column HOT 1
- createTableIfMissing is not working in version: 9.0.0 HOT 5
- TypeError: (0 , connect_pg_simple_1.default) is not a function HOT 2
- session_id is ignored and a new one is generated instead HOT 5
- Getting "Pool is missing for some Reasons", If not there is no communication between Client and server for more than 24 hours HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-connect-pg-simple.