Authentication with PostgreSQL for Baileys based on https://github.com/bobslavtriev/mysql-baileys
If you want with your specifications, if you don't create it, the code will automatically create
CREATE TABLE "auth" (
session VARCHAR(50) NOT NULL,
id VARCHAR(80) NOT NULL,
value TEXT DEFAULT NULL,
UNIQUE (session, id)
);
CREATE INDEX idx_auth_session ON "auth" (session);
CREATE INDEX idx_auth_id ON "auth" (id);npm i pg-baileys
const { usePGAuthState } = require('pg-baileys')const { state, saveCreds, removeCreds } = await usePGAuthState({
session: sessionName, // required
password: 'Password123#', // required
database: 'baileys', // required
})type PGConfig = {
/* The hostname of the database you are connecting to. (Default: localhost) */
host?: string,
/* The port number to connect to. (Default: 5432) */
port?: number,
/* The PostgreSQL user to authenticate as. (Default: postgres) */
user?: string,
/* The password of that PostgreSQL user */
password: string,
/* Name of the database to use for this connection. (Default: postgres) */
database: string,
/* PostgreSQL table name. (Default: auth) */
tableName?: string,
/* Retry the query at each interval if it fails. (Default: 200ms) */
retryRequestDelayMs?: number,
/* Maximum attempts if the query fails. (Default: 10) */
maxRetries?: number,
/* Session name to identify the connection, allowing multisessions with PostgreSQL. */
session: string,
/* Use the config SSL. (Default: disabled) */
ssl?: boolean | { rejectUnauthorized: boolean }
}import { makeWASocket, makeCacheableSignalKeyStore, fetchLatestBaileysVersion } from 'baileys'
import { usePGAuthState } from 'pg-baileys'
async function startSock(sessionName){
const { error, version } = await fetchLatestBaileysVersion()
if (error){
console.log(`Session: ${sessionName} | No connection, check your internet.`)
return startSock(sessionName)
}
const { state, saveCreds, removeCreds } = await usePGAuthState({
session: sessionName,
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'Password123#',
database: 'baileys',
tableName: 'auth'
})
const sock = makeWASocket({
auth: {
creds: state.creds,
keys: makeCacheableSignalKeyStore(state.keys, logger),
},
version: version,
defaultQueryTimeoutMs: undefined
})
sock.ev.on('creds.update', saveCreds)
sock.ev.on('connection.update', async({ connection, lastDisconnect }) => {
// your code here
})
sock.ev.on('messages.upsert', async({ messages, type }) => {
// your code here
})
}
startSock('session1')startSock('session1')
startSock('session2')
startSock('session3')
startSock('session4')MIT