N
Neon11mo ago
extended-salmon

Lua/pgmoon connection issues with Neon PostgreSQL endpoint ID configuration

Description: I'm trying to connect to a Neon PostgreSQL database from a Lua application using pgmoon. Im: * Attempting to establish a connection to Neon PostgreSQL from Lua using pgmoon library * Running on Replit with PostgreSQL 15 The reason why is because I need to store user data and achievements in a PostgreSQL database Here's my current connection code:
local pg = pgmoon.new({
host = os.getenv("PGHOST"),
port = 5432,
user = os.getenv("PGUSER"),
password = os.getenv("PGPASSWORD"),
database = os.getenv("PGDATABASE"),
ssl = true,
sslmode = "require"
})
local pg = pgmoon.new({
host = os.getenv("PGHOST"),
port = 5432,
user = os.getenv("PGUSER"),
password = os.getenv("PGPASSWORD"),
database = os.getenv("PGDATABASE"),
ssl = true,
sslmode = "require"
})
Error message:
database connection failed: ERROR: Endpoint ID is not specified. Either please upgrade the postgres client library (libpq) for SNI support or pass the endpoint ID (first part of the domain name) as a parameter: '?options=endpoint%3D<endpoint-id>'. See more at https://neon.tech/sni
database connection failed: ERROR: Endpoint ID is not specified. Either please upgrade the postgres client library (libpq) for SNI support or pass the endpoint ID (first part of the domain name) as a parameter: '?options=endpoint%3D<endpoint-id>'. See more at https://neon.tech/sni
What I've tried: * Adding endpoint_id parameter * Adding options parameter with encoded endpoint * Using database_url with endpoint options * Setting sslmode to require * Specifying the endpoint ID in the password field Extra information: * My phone is constantly on do not disturb, so if you have an answer, ping me * My timezone is Cst and I am available until 10 p.m.
10 Replies
extended-salmon
extended-salmon11mo ago
cc @Conrad Ludgate
conscious-sapphire
conscious-sapphire11mo ago
Hi. Sorry you're experiencing issues. Setting the endpoint_id in the options or in the password field should work, so this is very perplexing Just to confirm, did you set
password = "endpoint=ep-whatever-1234$".. os.getenv("PGPASSWORD"),
password = "endpoint=ep-whatever-1234$".. os.getenv("PGPASSWORD"),
extended-salmon
extended-salmonOP11mo ago
Yes, I did. Also this just gives me "connection refused"
local function connectToDatabase()
local pg = pgmoon.new({
database_url = os.getenv("DATABASE_URL"),
user = os.getenv("PGUSER"),
password = "endpoint=" .. os.getenv("PGEP") .. "$" .. os.getenv("PGPASSWORD"),
os.getenv("PGHOST"),
os.getenv("PGDATABASE")
})

local success, err = assert(pg:connect())
if not success then
print("database connection failed: " .. (err or "unknown error"))
return nil
end

local _, timeoutErr = pg:query("SET statement_timeout = '30000'")
if timeoutErr then print("warning: could not set statement timeout") end

return pg
end
local function connectToDatabase()
local pg = pgmoon.new({
database_url = os.getenv("DATABASE_URL"),
user = os.getenv("PGUSER"),
password = "endpoint=" .. os.getenv("PGEP") .. "$" .. os.getenv("PGPASSWORD"),
os.getenv("PGHOST"),
os.getenv("PGDATABASE")
})

local success, err = assert(pg:connect())
if not success then
print("database connection failed: " .. (err or "unknown error"))
return nil
end

local _, timeoutErr = pg:query("SET statement_timeout = '30000'")
if timeoutErr then print("warning: could not set statement timeout") end

return pg
end
Also all the environmental variables are set
extended-salmon
extended-salmon11mo ago
I don't you would set database_url in that snippet since you broke it apart ^. I'm not familiar with pgmoon. Do you need host and database table entries?
extended-salmon
extended-salmonOP11mo ago
i based it off of the github page in example
extended-salmon
extended-salmonOP11mo ago
GitHub
GitHub - leafo/pgmoon: A pure Lua Postgres driver for use in OpenRe...
A pure Lua Postgres driver for use in OpenResty & more - leafo/pgmoon
extended-salmon
extended-salmonOP11mo ago
I kind of just tried everything but that's where I left off. I don't think I need the database_url in there either. I got it to work:
local pg = pgmoon.new({
host = os.getenv("PGHOST"),
port = os.getenv("PGPORT"),
database = os.getenv("PGDATABASE"),
user = os.getenv("PGUSER"),
password = "endpoint=" .. os.getenv("PGEP") .. "$" .. os.getenv("PGPASSWORD"),
ssl = "require"
})
local pg = pgmoon.new({
host = os.getenv("PGHOST"),
port = os.getenv("PGPORT"),
database = os.getenv("PGDATABASE"),
user = os.getenv("PGUSER"),
password = "endpoint=" .. os.getenv("PGEP") .. "$" .. os.getenv("PGPASSWORD"),
ssl = "require"
})
It turns out I had to put it in that specific order lol
conscious-sapphire
conscious-sapphire11mo ago
Oh, that's weird. Thanks for the update I'll try and contribute a patch to pgmoon so the "password hack" isn't necessary next week
extended-salmon
extended-salmonOP11mo ago
Ok Now I just have to go based off of the database table and update my code to handle it (it would be so much easier if I remembered the SQL code I made to make the database)😭
extended-salmon
extended-salmon11mo ago
pg_dump

Did you find this page helpful?