Pharo connect to PostgreSQL

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Pharo connect to PostgreSQL

Pharo Smalltalk Users mailing list
I am trying to connect to a PostgreSQL database from Pharo but I keep getting
"SubscriptOutOfBounds: 11" error (I am a beginner). The server is started
and the database is working fine. What am I missing?


|connectionArguments connection resultSets resultSet rows firstRow|

connectionArguments := PG3ConnectionArguments new
        hostname: 'localhost';
        port: 5432;
        username: 'postgres';
        password: 'lucian';
        databaseName: 'mydb'
        yourself.
connection := connectionArguments newConnection.

connection startup.

resultSets := connection execute: 'select 3 + 4, now()'.
resultSet := resultSets first.
rows := resultSet rows.
firstRow := rows first.
firstRow at: 1.
firstRow at: 2.

connection terminate.



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Pharo connect to PostgreSQL

Sven Van Caekenberghe-2
Hi,

You are not saying which library/framework you are trying to use.

One up to date option is P3, the "lean and mean PostgreSQL client for Pharo " [ https://github.com/svenvc/P3 ].

A connection to your database could then be specified using a URL.

To test the connection:

(P3Client url: 'psql://postgres:lucian@localhost:5432/mydb') in: [ :client | [ client isWorking ] ensure: [ client close ] ].

To do a query:

(P3Client url: 'psql://postgres:lucian@localhost:5432/mydb') in: [ :client | [ (client query: 'SELECT * FROM some_table') recordsAsDictionaries ] ensure: [ client close ] ].

Of course, you first have to make sure external clients can connect to your db (try the psql command line client over TCP first).

HTH,

Sven

Apart from https://postgresql.org, the following site is pretty good: https://www.postgresqltutorial.com. On macOS, I recommend https://postgresapp.com.

> On 20 Jan 2020, at 12:37, Macmus via Pharo-users <[hidden email]> wrote:
>
>
> From: Macmus <[hidden email]>
> Subject: Pharo connect to PostgreSQL
> Date: 20 January 2020 at 12:37:13 GMT+1
> To: [hidden email]
>
>
> I am trying to connect to a PostgreSQL database from Pharo but I keep getting
> "SubscriptOutOfBounds: 11" error (I am a beginner). The server is started
> and the database is working fine. What am I missing?
>
>
> |connectionArguments connection resultSets resultSet rows firstRow|
>
> connectionArguments := PG3ConnectionArguments new
> hostname: 'localhost';
> port: 5432;
> username: 'postgres';
> password: 'lucian';
> databaseName: 'mydb'
> yourself.
> connection := connectionArguments newConnection.
>
> connection startup.
>
> resultSets := connection execute: 'select 3 + 4, now()'.
> resultSet := resultSets first.
> rows := resultSet rows.
> firstRow := rows first.
> firstRow at: 1.
> firstRow at: 2.
>
> connection terminate.
>
>
>
> --
> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
>
>
>