pglite icon indicating copy to clipboard operation
pglite copied to clipboard

Unable to connect using `Postgrex` .

Open itsWill opened this issue 2 months ago • 5 comments

I'm trying to connect to pglite from an elixir application.

When connecting through TCP settings or unix sockets to the the PGLite started with: npx pglite-server --path=/tmp/.s.PGSQL.5432

I get the following error:

Interactive Elixir (1.18.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(2)> Postgrex.start_link(socket_dir: "/tmp", port: 5432, username: "postgres", password: "postgres", database: "postgres", show_sensitive_data_on_connection_error: true)
{:ok, #PID<0.213.0>}

00:43:24.134 [error] :gen_statem #PID<0.215.0> terminating
** (FunctionClauseError) no function clause matching in String.split/3
    (elixir 1.18.4) lib/string.ex:504: String.split(nil, " ", [parts: 2])
    (postgrex 0.21.1) lib/postgrex/utils.ex:72: Postgrex.Utils.parse_version/1
    (postgrex 0.21.1) lib/postgrex/protocol.ex:1062: Postgrex.Protocol.bootstrap_send/3
    (postgrex 0.21.1) lib/postgrex/protocol.ex:749: Postgrex.Protocol.handshake/3
    (postgrex 0.21.1) lib/postgrex/protocol.ex:229: Postgrex.Protocol.connect_endpoints/6
    (db_connection 2.8.1) lib/db_connection/connection.ex:79: DBConnection.Connection.handle_event/4
    (stdlib 6.2) gen_statem.erl:3737: :gen_statem.loop_state_callback/11
    (stdlib 6.2) proc_lib.erl:329: :proc_lib.init_p_do_apply/3
Process Label: "db_conn_1"
Queue: [internal: {:connect, :init}]
Postponed: []
State: Postgrex.Protocol
Callback mode: :handle_event_function, state_enter: false

notably:

(elixir 1.18.4) lib/string.ex:504: String.split(nil, " ", [parts: 2])
    (postgrex 0.21.1) lib/postgrex/utils.ex:72: Postgrex.Utils.parse_version/1

Makes me think that the version is not being passed back to pglite during handshake to Postgrex.

However within PGLite I can see the version:

=> SHOW server_version;
 server_version
----------------
 17.5
(1 row)

This is the relevant line in Postgrex: https://github.com/elixir-ecto/postgrex/blob/251f30b6c4eb8e642eaec990d96059ec18f81ba3/lib/postgrex/protocol.ex#L1062

itsWill avatar Oct 18 '25 04:10 itsWill

Related https://github.com/electric-sql/pglite/issues/708

tdrz avatar Oct 18 '25 06:10 tdrz

Possible cause: during the initial handshake, PostgreSQL will report GUC Options but only if they haven't been reported already.

static void
ReportGUCOption(struct config_generic *record)
{
	char	   *val = ShowGUCOption(record, false);

	if (record->last_reported == NULL ||
		strcmp(val, record->last_reported) != 0)
	{

tdrz avatar Nov 03 '25 15:11 tdrz

@itsWill Is it possible to set Postgrex (I guess through https://github.com/elixir-ecto/db_connection) to use a single connection for all the queries?

PGlite is single connection at the moment, so any client that is trying to connect but relies on multiple connections will not work at the moment.

tdrz avatar Nov 05 '25 08:11 tdrz

@itsWill please try again with the latest PGlite version.

tdrz avatar Nov 05 '25 20:11 tdrz

Thanks @tdrz I'll have a look this weekend.

itsWill avatar Nov 07 '25 18:11 itsWill