Connecting to postgres on OSX from Swift using PostgresClientKit

Started by Howard Coleover 3 years ago11 messagesgeneral
Jump to latest
#1Howard Cole
howardnews@selestial.com

Hi,

Wondered if anyone had experience connection to pg14.1 running on OSX
using the swift library PostgresClientKit.

I am having difficulties connecting and hopefully someone can put me on
the right track.

The following is the swift code I am using

[Code]
var config = ConnectionConfiguration()
config.database = "mydb"
config.user = "postgres"
config.credential = .scramSHA256(password: "mypassword")
config.port = "5432"
config.ssl = true
let connection = try PostgresClientKit.Connection(configuration:
config)
defer { connection.close() }
[/Code]

I have the following in my pg_hba.conf file:

[Code]
local all all scram-sha-256
[/Code]

I can successfully connect using psql and/pgadmin, but when I try and
connect in code I get the following error:

[Code]
[Connection-1 Severe] Unable to connect socket: Error Code:
-9989(0x2705), Operation not permitted
[/Code]

Any suggestions?

Thanks

Howard Cole
Selestial.com

#2Rob Sargent
robjsargent@gmail.com
In reply to: Howard Cole (#1)
Re: Connecting to postgres on OSX from Swift using PostgresClientKit

On 7/31/22 16:40, howardnews@selestial.com wrote:

Hi,

Wondered if anyone had experience connection to pg14.1 running on OSX
using the swift library PostgresClientKit.

I am having difficulties connecting and hopefully someone can put me
on the right track.

The following is the swift code I am using

[Code]
    var config = ConnectionConfiguration()
    config.database = "mydb"
    config.user = "postgres"
    config.credential = .scramSHA256(password: "mypassword")
    config.port = "5432"
    config.ssl = true
    let connection = try PostgresClientKit.Connection(configuration:
config)
    defer { connection.close() }
[/Code]

I have the following in my pg_hba.conf file:

[Code]
local     all     all                  scram-sha-256
[/Code]

I can successfully connect using psql and/pgadmin, but when I try and
connect in code I get the following error:

[Code]
[Connection-1 Severe] Unable to connect socket: Error Code:
-9989(0x2705), Operation not permitted
[/Code]

Any suggestions?

Thanks

Howard Cole
Selestial.com

Maybe add config.host = "localhost"?

#3Howard Cole
howardnews@selestial.com
In reply to: Rob Sargent (#2)
Re: Connecting to postgres on OSX from Swift using PostgresClientKit

On 2022-07-31 23:42, Rob Sargent wrote:

On 7/31/22 16:40, howardnews@selestial.com wrote:

Hi,

Wondered if anyone had experience connection to pg14.1 running on
OSX using the swift library PostgresClientKit.

I am having difficulties connecting and hopefully someone can put me
on the right track.

The following is the swift code I am using

[Code]
var config = ConnectionConfiguration()
config.database = "mydb"
config.user = "postgres"
config.credential = .scramSHA256(password: "mypassword")
config.port = "5432"
config.ssl = true
let connection = try PostgresClientKit.Connection(configuration:
config)
defer { connection.close() }
[/Code]

I have the following in my pg_hba.conf file:

[Code]
local all all scram-sha-256
[/Code]

I can successfully connect using psql and/pgadmin, but when I try
and connect in code I get the following error:

[Code]
[Connection-1 Severe] Unable to connect socket: Error Code:
-9989(0x2705), Operation not permitted
[/Code]

Any suggestions?

Thanks

Howard Cole
Selestial.com

Maybe add config.host = "localhost"?

Thanks, tried localhost, 127.0.0.1 and ::128. No joy

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Howard Cole (#3)
Re: Connecting to postgres on OSX from Swift using PostgresClientKit

howardnews@selestial.com writes:

On 2022-07-31 23:42, Rob Sargent wrote:

On 7/31/22 16:40, howardnews@selestial.com wrote:

I can successfully connect using psql and/pgadmin, but when I try
and connect in code I get the following error:
[Connection-1 Severe] Unable to connect socket: Error Code:
-9989(0x2705), Operation not permitted

Maybe add config.host = "localhost"?

Thanks, tried localhost, 127.0.0.1 and ::128. No joy

Don't know much about Swift, but it wouldn't surprise me if that
code is running in some kind of sandbox that disallows IP
connections by default.

Also, does psql work when you say "psql -h localhost"? It's
possible that your server isn't listening on localhost, or
there's a firewall there. Your working connections could all be
via Unix sockets, from what little you've said so far.

regards, tom lane

#5Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Howard Cole (#3)
Re: Connecting to postgres on OSX from Swift using PostgresClientKit

On 7/31/22 16:53, howardnews@selestial.com wrote:

On 2022-07-31 23:42, Rob Sargent wrote:

On 7/31/22 16:40, howardnews@selestial.com wrote:

Hi,

Wondered if anyone had experience connection to pg14.1 running on
OSX using the swift library PostgresClientKit.

I am having difficulties connecting and hopefully someone can put me
on the right track.

Thanks

Howard Cole
Selestial.com

 Maybe add config.host = "localhost"?

Thanks, tried localhost, 127.0.0.1 and ::128. No joy

What error do you get in the above cases?

--
Adrian Klaver
adrian.klaver@aklaver.com

#6Howard Cole
howardnews@selestial.com
In reply to: Tom Lane (#4)
Re: Connecting to postgres on OSX from Swift using PostgresClientKit

On 2022-08-01 01:02, Tom Lane wrote:

howardnews@selestial.com writes:

On 2022-07-31 23:42, Rob Sargent wrote:

On 7/31/22 16:40, howardnews@selestial.com wrote:

I can successfully connect using psql and/pgadmin, but when I try
and connect in code I get the following error:
[Connection-1 Severe] Unable to connect socket: Error Code:
-9989(0x2705), Operation not permitted

Maybe add config.host = "localhost"?

Thanks, tried localhost, 127.0.0.1 and ::128. No joy

Don't know much about Swift, but it wouldn't surprise me if that
code is running in some kind of sandbox that disallows IP
connections by default.

Swift/Xcode is new to me too!

Also, does psql work when you say "psql -h localhost"? It's
possible that your server isn't listening on localhost, or
there's a firewall there. Your working connections could all be
via Unix sockets, from what little you've said so far.

regards, tom lane

Using psql, i can successfully connect to localhost. In the
postgresql.conf the listen address is *.
The application was installed from the EDB installer; not sure about
firewalls on sandboxes in this situation.

Howard

#7Howard Cole
howardnews@selestial.com
In reply to: Adrian Klaver (#5)
Re: Connecting to postgres on OSX from Swift using PostgresClientKit

On 2022-08-01 01:04, Adrian Klaver wrote:

On 7/31/22 16:53, howardnews@selestial.com wrote:

On 2022-07-31 23:42, Rob Sargent wrote:

On 7/31/22 16:40, howardnews@selestial.com wrote:

Hi,

Wondered if anyone had experience connection to pg14.1 running on
OSX using the swift library PostgresClientKit.

I am having difficulties connecting and hopefully someone can put me
on the right track.

Thanks

Howard Cole
Selestial.com

 Maybe add config.host = "localhost"?

Thanks, tried localhost, 127.0.0.1 and ::128. No joy

What error do you get in the above cases?

Here is the error I get in the application.

2022-08-01T00:20:18.530Z Connection-1 finer] Created socket
[2022-08-01T00:20:18.532Z Connection-1 fine] Opening connection to port
5432 on host localhost
2022-08-01 01:20:18.534308+0100 TestPostgres[10654:120173]
dnssd_clientstub ConnectToServer: connect() failed
path:/var/run/mDNSResponder Socket:13 Err:-1 Errno:1 Operation not
permitted
2022-08-01 01:20:18.535617+0100 TestPostgres[10654:120173]
[si_destination_compare] socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL)
failed: Operation not permitted
2022-08-01 01:20:18.535694+0100 TestPostgres[10654:120173]
[si_destination_compare] socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL)
failed: Operation not permitted
[2022-08-01T00:20:18.537Z Connection-1 severe] Unable to connect socket:
Error code: -9989(0x-2705), Operation not permitted
[2022-08-01T00:20:18.537Z Connection-1 finer] Closing socket

#8Howard Cole
howardnews@selestial.com
In reply to: Adrian Klaver (#5)
Re: Connecting to postgres on OSX from Swift using PostgresClientKit

On 2022-08-01 01:04, Adrian Klaver wrote:

On 7/31/22 16:53, howardnews@selestial.com wrote:

On 2022-07-31 23:42, Rob Sargent wrote:

On 7/31/22 16:40, howardnews@selestial.com wrote:

Hi,

Wondered if anyone had experience connection to pg14.1 running on
OSX using the swift library PostgresClientKit.

I am having difficulties connecting and hopefully someone can put me
on the right track.

Thanks

Howard Cole
Selestial.com

 Maybe add config.host = "localhost"?

Thanks, tried localhost, 127.0.0.1 and ::128. No joy

What error do you get in the above cases?

When using 127.0.0.1...

2022-08-01T00:29:19.319Z Connection-1 finer] Created socket
[2022-08-01T00:29:19.322Z Connection-1 fine] Opening connection to port
5432 on host 127.0.0.1
[2022-08-01T00:29:19.324Z Connection-1 severe] Unable to connect socket:
Error code: -9989(0x-2705), Operation not permitted
[2022-08-01T00:29:19.324Z Connection-1 finer] Closing socket

#9Ron
ronljohnsonjr@gmail.com
In reply to: Howard Cole (#8)
Re: Connecting to postgres on OSX from Swift using PostgresClientKit

On 7/31/22 19:30, howardnews@selestial.com wrote:

On 2022-08-01 01:04, Adrian Klaver wrote:

On 7/31/22 16:53, howardnews@selestial.com wrote:

On 2022-07-31 23:42, Rob Sargent wrote:

On 7/31/22 16:40, howardnews@selestial.com wrote:

Hi,

Wondered if anyone had experience connection to pg14.1 running on
OSX using the swift library PostgresClientKit.

I am having difficulties connecting and hopefully someone can put me
on the right track.

Thanks

Howard Cole
Selestial.com

 Maybe add config.host = "localhost"?

Thanks, tried localhost, 127.0.0.1 and ::128. No joy

What error do you get in the above cases?

When using 127.0.0.1...

2022-08-01T00:29:19.319Z Connection-1 finer] Created socket
[2022-08-01T00:29:19.322Z Connection-1 fine] Opening connection to port
5432 on host 127.0.0.1
[2022-08-01T00:29:19.324Z Connection-1 severe] Unable to connect socket:
Error code: -9989(0x-2705), Operation not permitted
[2022-08-01T00:29:19.324Z Connection-1 finer] Closing socket

This really looks like an error somewhere in the Mac OS networking stack.

What happens when you "telnet localhost 5432"?

--
Angular momentum makes the world go 'round.

#10Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Howard Cole (#7)
Re: Connecting to postgres on OSX from Swift using PostgresClientKit

On 7/31/22 17:27, howardnews@selestial.com wrote:

On 2022-08-01 01:04, Adrian Klaver wrote:

On 7/31/22 16:53, howardnews@selestial.com wrote:

On 2022-07-31 23:42, Rob Sargent wrote:

2022-08-01T00:20:18.530Z Connection-1 finer] Created socket
[2022-08-01T00:20:18.532Z Connection-1 fine] Opening connection to port
5432 on host localhost
2022-08-01 01:20:18.534308+0100 TestPostgres[10654:120173]
dnssd_clientstub ConnectToServer: connect() failed

A search on above error message found:

https://github.com/firebase/firebase-ios-sdk/issues/3033

https://developer.apple.com/forums/thread/67792

https://github.com/emqx/CocoaMQTT/issues/202

all of which point to what Tom Lane mentioned, sandboxing.

path:/var/run/mDNSResponder Socket:13 Err:-1 Errno:1 Operation not
permitted
2022-08-01 01:20:18.535617+0100 TestPostgres[10654:120173]
[si_destination_compare] socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL)
failed: Operation not permitted
2022-08-01 01:20:18.535694+0100 TestPostgres[10654:120173]
[si_destination_compare] socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL)
failed: Operation not permitted
[2022-08-01T00:20:18.537Z Connection-1 severe] Unable to connect socket:
Error code: -9989(0x-2705), Operation not permitted
[2022-08-01T00:20:18.537Z Connection-1 finer] Closing socket

--
Adrian Klaver
adrian.klaver@aklaver.com

#11Howard Cole
howardnews@selestial.com
In reply to: Adrian Klaver (#10)
Re: Connecting to postgres on OSX from Swift using PostgresClientKit

On 2022-08-01 04:06, Adrian Klaver wrote:

2022-08-01T00:20:18.530Z Connection-1 finer] Created socket
[2022-08-01T00:20:18.532Z Connection-1 fine] Opening connection to
port 5432 on host localhost
2022-08-01 01:20:18.534308+0100 TestPostgres[10654:120173]
dnssd_clientstub ConnectToServer: connect() failed

A search on above error message found:

https://github.com/firebase/firebase-ios-sdk/issues/3033

https://developer.apple.com/forums/thread/67792

https://github.com/emqx/CocoaMQTT/issues/202

all of which point to what Tom Lane mentioned, sandboxing.

path:/var/run/mDNSResponder Socket:13 Err:-1 Errno:1 Operation not
permitted
2022-08-01 01:20:18.535617+0100 TestPostgres[10654:120173]
[si_destination_compare] socket(PF_SYSTEM, SOCK_DGRAM,
SYSPROTO_CONTROL) failed: Operation not permitted
2022-08-01 01:20:18.535694+0100 TestPostgres[10654:120173]
[si_destination_compare] socket(PF_SYSTEM, SOCK_DGRAM,
SYSPROTO_CONTROL) failed: Operation not permitted
[2022-08-01T00:20:18.537Z Connection-1 severe] Unable to connect
socket: Error code: -9989(0x-2705), Operation not permitted
[2022-08-01T00:20:18.537Z Connection-1 finer] Closing socket

Hi Adrian,

you & Tom were correct - it was sandbox settings (which was turned
off??) in Xcode which caused the problems. In case anyone else has the
problem, the steps I took to fix the problem in XCode 13.3 were as
follows:

1. Go to the project explorer and select the project.
2. Select the Target under the list of Targets
3. Select the "Signing & Capabilities" Tab/Heading
4. Under the "App Sandbox" settings, select the "Outgoing connections
(client)" checkbox.
5. Note - the "Enable App Sandbox" under Build Settings seems to have no
effect!

Apologies for not finding this myself - This was purely a MacOS / XCode
problem - but many thanks to you all (Adrian, Tom, Rob) for proving yet
again that Postgresql support is the best.

Howard.