# Require 'postgres'

**URL:** <https://www.ruby-forum.com/t/require-postgres/126390>\
**Category:** Rails\
**Created:** [January 17, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/require-postgres/126390 "2008-01-17T22:22:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [January 17, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/require-postgres/126390/1 "2008-01-17T22:22:43Z")

</div>

hi,

I’m trying to write a test application that queries a postgres  
database, which so far I’ve managed to do but in a limited way. I’m  
completely lost how to perform transactions and how to return the  
primary key id for newly inserted rows. hopefully by placing some  
code someone might be able to help,

require ‘postgres’

db = PGconn.connect(‘localhost’,  
5432,’’,’’,‘testdatabase’,‘username’,‘password’)  
results = db.exec(‘select \* from testtable’)

I can then loop on results and display them using puts.

However suppose i wanted to issue the following statement but within a  
transaction? I simply can’t find any documentation anywhere to help  
me

db.exec(‘delete from testtable’)

in addition, suppose i was to insert a row into testtable and want the  
id returned back? i can’t figure that out either.

can anyone help?

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [January 18, 2008, 12:14am UTC](https://www.ruby-forum.com/t/require-postgres/126390/2 "2008-01-18T00:14:24Z")

</div>

Are you trying to do this in Rails?

You know you could use the config/database.yml to set up the database  
and  
install the connector. You’re complicating things that don’t need  
complicating.

On Jan 18, 2008 7:51 AM, hiddenhippo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> db.exec(‘delete from testtable’)
> 
> in addition, suppose i was to insert a row into testtable and want the  
> id returned back? i can’t figure that out either.
> 
> can anyone help?
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [January 18, 2008, 9:55am UTC](https://www.ruby-forum.com/t/require-postgres/126390/3 "2008-01-18T09:55:02Z")

</div>

I’m trying to do it outside of rails, so purely in ruby, but there’s a  
distinct lack of documentation covering postgres within ruby.
