Skip to content

Connection Documentation: Peer Conncetion #1797

Description

@jasper-lyons-rhul

Hey! Thanks for your work on a great project.

I went looking for docs on how to manage postgresql's peer authentication. It's mentioned in #613 which provides a solution. Unfortunately that doesn't appear in the documentation. I wondered if we could add a section:

Password-less Authentication (Peer Auth)

Postgresql supports a number of authentication procedures which can be helpful, especially in development. One of those is Peer Authentication which allows the current OS user to authenticate with a local database. For this to work though, the connection need to be "local", specifically that means using a unix socket instead of a TCP socket.

To connect to the unix socket provided by postgresql you just need to use the path to the directory containing the socket as the host to your client configuration, this directory is commonly /var/run/postgresql. The client will then connect to the database mydb on the local postgresql server with the OS user that ran the program.

const { Pool, Client } = require('pg')

const pool = new Pool({
  host: '/var/run/postgresql',
  database: 'mydb'
})

pool.query('SELECT NOW()', (err, res) => {
  console.log(err, res)
  pool.end()
})

const client = new Client({
  host: '/var/run/postgresql',
  database: 'mydb'
})
client.connect()

client.query('SELECT NOW()', (err, res) => {
  console.log(err, res)
  pool.end()
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions