Connect session in PostgreSQL with SailsJS - postgresql

I have a personal project in SailsJS, Passport-Local and PostgreSQL. I managed to get connection and save users in PostgreSQL DB. But I have big problem with session storing. From what I know SailsJS is using Connect to get it done.
Previous working code for MongoDB:
connections: {
prodMongodbServer: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
//user: '',
//password: '',
database: 'sails-auth'
}
},
session: {
adapter: 'mongo',
host: 'localhost',
port: 27017,
db: 'sails-auth',
collection: 'sessions'
}
My (working) connection to PostgreSQL:
connections: {
somePostgresqlServer: {
adapter: 'sails-postgresql',
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'Password1',
database: 'sails-auth'
}
},
And the code I have problem with:
session: {
adapter: 'connect-pg-simple',
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'Password1',
db: 'sails-auth'
}
I've installed connect-pg-simple, executed SQL code but when I am starting sails app I get an error:
$ sails lift
info: Starting app...
Init Express midleware
Failed to prune sessions: password authentication failed for user "myPcUser"
info:
info: .-..-.
info:
info: Sails <| .-..-.
info: v0.11.3 |\
info: /|.\
info: / || \
info: ,' |' \
info: .-'.-==|/_--'
info: `--'-------'
info: __---___--___---___--___---___--___
info: ____---___--___---___--___---___--___-__
info:
I thought that it may be beetter to use connect-pg, but it requires so many dependecy that I stayed with simple version. Anyone know what this problem is about? Im on windows so its weird that it have problem with my PC User...
Or maybe someone have some solution for using PostgreSQL for users, etc and Mongo for sessions. For what I've tested Passport doesnt allow to do that (user can sign up but can't sign in)

Perhaps you can fix this error open PostgreSQL client authentication configuration file:
/var/lib/pgsql/data/pg_hba.conf
and change:
local all all trust
host all 127.0.0.1/32 trust
Save and close the file. Restart Postgresql server:
service postgresql restart

To make it work
Install the connect-pg-simple node module: $ npm install connect-pg-simple
In session.js define the adapter and connection string:
adapter: 'connect-pg-simple',
conString: 'postgres://user:pass#host:5432/dbname',
3) run the SQL commands on file node_modules/connect-pg-simple/table.sql to create the table for sessions. :
CREATE TABLE "session" (
"sid" varchar NOT NULL COLLATE "default",
"sess" json NOT NULL,
"expire" timestamp(6) NOT NULL
)
WITH (OIDS=FALSE);
ALTER TABLE "session" ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE;
CREATE INDEX "IDX_session_expire" ON "session" ("expire");

I used for that sails-pg-session.
The information about sessions must be stored in config/session.js:
adapter: 'sails-pg-session',
database: 'dbname',
host: 'localhost',
user: 'dbuser',
password: 'dbpasswd',
port: 5432
Hope it helps

Related

Mongodb Compass: Connection timed out

I installed Mongodb on my remote server using this documentation. I have Ubuntu 16.04 on my remote server. Mongodb got installed successfully. I added the user like this:
use admin
db.createUser(
{
user: 'myuser',
pwd: 'password',
roles: [ { role: 'readWrite', db: 'mydb' } ]
}
);
I also made changes in the mongod.conf like this:
net:
port: 27017
bindIp: 127.0.0.1,<server_ip>
security:
authorization: 'enabled'
Now when I try to connect to mongodb using conneciton string like this:
mongodb://myuser:password#server_ip:27017/mydb
It gives me the following error:
connection timed out
What am I doing wrong here? I am using Laravel Forge to manage sever.
As it turned out the port was not open and that was the only issue. Opened the port and now its working fine.

Cannot connect to Postgres Database from Strapi on Google App Engine

Need help connecting to my Google SQL Postgres Database from Google App Engine. I've followed https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/deployment/hosting-guides/google-app-engine.html but keep running into the following error:
2022-09-27 16:11:32 default[20220928t020816] [2022-09-27 16:11:32.307] debug: ⛔️ Server wasn't able to start properly.
2022-09-27 16:11:32 default[20220928t020816] [2022-09-27 16:11:32.309] error: connect ECONNREFUSED /cloudsql/project-id:us-central1:database-id/.s.PGSQL.5432
database.ts
export default ({ env }) => ({
connection: {
client: 'postgres',
connection: {
host: env('DATABASE_HOST', '127.0.0.1'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'database-id'),
user: env('DATABASE_USERNAME', 'strapi'),
password: env('DATABASE_PASSWORD', 'strapi'),
ssl: env.bool('DATABASE_SSL', false),
},
},
});
app.yaml
runtime: nodejs16
instance_class: F1
env_variables:
ADMIN_JWT_SECRET: 'XXXXXXXXXXXXX'
API_TOKEN_SALT: 'XXXXXXXXXXXXX'
APP_KEYS: 'XXXXXXXXXXXXX'
DATABASE_HOST: '/cloudsql/project-id:us-central1:database-id'
DATABASE_PORT: '5432'
DATABASE_NAME: 'database-id'
DATABASE_USERNAME: 'postgres'
DATABASE_PASSWORD: 'XXXXXXXXXXXXX'
DATABASE_SSL: 'false'
HOST: '0.0.0.0'
JWT_SECRET: 'XXXXXXXXXXXXX'
NODE_ENV: 'production'
PORT: '1337'
beta_settings:
cloud_sql_instances: 'project-id:us-central1:database-id'
One of the causes of error: connect ECONNREFUSED and the most common one when connecting to Cloud SQL using Unix Sockets (as is the case here) is that your GCP project does not have the Cloud SQL Admin API enabled.
Enable the Cloud SQL Admin API and that should help resolve the connectivity issue.

Configuring sails app for connection to Heroku Postgres using Environment Variables

Can a sails.js database connection be configured using a URI, or a URL which incorporates the credentials (host, user, password, and database) into one string? Or do they need to be individually assigned as their own key-value pairs in the configuration?
In development, I have configured my sails.js app to use environment variables for database credentials, as such:
in connections.js:
someSqlDatabase: {
host: process.env.dbHost,
user: process.env.dbUserName,
password: process.env.dbPassword,
database: process.env.db,
port: 5432,
ssl: true,
adapter: 'sails-postgresql'
This works for my connection to a heroku-postgresql resource that I've allocated to my project, and I am able to manually set "Config Vars" in my heroku instance, which I can use in the same way as environment variables in my local instance. By this I mean I can go to the heroku client page for the database resource, copy its credentials (host, database, user, and password) into the heroku app's configuration variables, individually.
The problem is that heroku periodically changes the credentials, and that my manually-copied configuration variables will become outdated, and require re-pasting whenever this happens. Obviously, this is not an ideal solution.
Heroku does automatically provide one configuration variable for the resource, called DATABASE_URL, which looks like:
"postgres://<user>:<password>#<host>:<port>/<database>"
which should be automatically updated, when the credentials change.
My problem is that I don't know how to configure a sails.js connection to use this url, in the place of individual "host", "user", "password", and "database" key/value pairs in the the connection config.
I've tried using a configuration that omits the individual keys and uses only "host", as such:
someSqlDatabase: {
host: process.env.DATABASE_URL,
ssl: true,
adapter: 'sails-postgresql'
}
But this connection fails. I've seen this kind of string called a URI, and I've tried the long-shot configuration:
someSqlDatabase: {
uri: proccess.env.DATABASE_URL
ssl: true,
adapter: 'sails-postgresql'
}
but this fails, as well. Is there a way to configure a sails-js connection with heroku postgresql database that will automatically use the latest credentials? (I am using sails v0.12.3)
I am not using PostgreSQL, but you could try/adapt this:
// before/outside module.exports
var url = require('url').parse(process.env.DATABASE_URL);
// within the config
someSqlDatabase: {
host: url.host,
user: url.auth.split(':')[0],
password: url.auth.split(':')[1],
database: url.pathname.substring(1),
port: url.port,
ssl: true,
adapter: 'sails-postgresql'
}
The following is probably the correct way to do this :
module.exports = {
...
default: {
ssl: true,
url: process.env.HEROKU_POSTGRESQL_SILVER_URL,
adapter: 'sails-postgresql'
},
...
}
inside config/env/production.js

sails-sqlserver: multiple sql servers on same host

I'm using sails-sqlserver where my host is running both SQL Server 2008 and 2012. In connections.js - host: is set to 'SQLServerhost\2012' I am getting the error "error: A hook (orm) failed to load!" Is there a different way to specify which SQL Server to connect to?
I've found success with the below format in /config/connections.js when I need to work with different databases on the same server (but see below suggestion):
sqlserverLOCAL: {
adapter: 'sails-sqlserver',
user: 'username',
password: 'password',
host: 'localhost', // Name or IP Address of SQL Server
port: '1433',
database: 'YourDBName' // i.e. Initial Catalog
},
sqlserver: {
adapter: 'sails-sqlserver',
user: 'username',
password: 'password',
host: '11.222.333.444', // IP Address of SQL Server
port: '1433',
database: 'OtherDBName' // i.e. Initial Catalog
}
In your case, perhaps setting both host: attributes to the same server (just SQLServerhost rather than SQLServerhost\2012) and then set the database: attribute to the appropriate Initial Catalog would be best. Also, make sure you double-check what ports the two SQL Servers are running on and set that attribute value accordingly.

How to use mongodb-express with MeteorJS' MongoDB?

I'm building an application in MeteorJS.
I want to have GUI access to built-in MongoDB database.
So I found: https://github.com/andzdroid/mongo-express
I installed it, configured it to connect to localhost:3001.
Since mongodb doesn't have a default admin password, I tried to create it by:
meteor mongo
use admin
db.addUser("admin","password")
then I set
adminUsername: 'admin',
adminPassword: 'password',
in mongo-express\config.js.
However when I open localhost:8081, it asks me login credentials again and even if I insert them manaully (admin, password) it doesn't work.
So I went back to meteor mongo, tried to create admin user again and go error
Error: couln't add user: User admin#admin" already exists
What am I doing wrong?
edit /usr/local/lib/node_modules/mongo-express/config.default.js
find and edit to:
} else {
mongo = {
db: 'meteor',
host: 'localhost',
password: '',
port: 3001,
ssl: false,
url: 'mongodb://localhost:3001/meteor',
username: '',
};
}