{"id":1337,"date":"2012-10-28T09:20:55","date_gmt":"2012-10-28T03:20:55","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=1337"},"modified":"2016-01-07T11:31:04","modified_gmt":"2016-01-07T05:31:04","slug":"php-postgres","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1337","title":{"rendered":"PHP &#038; Postgres"},"content":{"rendered":"<p>I have been using PHP for a while now. \u00c2\u00a0When people say PHP, its usually associated with \u00c2\u00a0 \u00c2\u00a0 MySQL as its backend database. But PHP will work with pretty much any database. \u00c2\u00a0Few years ago I wrote about how to get <a href=\"https:\/\/techsatwork.com\/blog\/?p=145\" target=\"_blank\">PHP to work with DB2<\/a>. \u00c2\u00a0 Here I am going to attempt to explain how to configure PHP to work with Postgres. \u00c2\u00a0For those of you not heard of <a href=\"http:\/\/www.postgresql.org\" target=\"_blank\">Postgres<\/a>, its another famous open source database. Its highly scalable and stable. Postgres can run pretty much on any operating systems. \u00c2\u00a0I have been trying to get more familiar with it and potentially write an application that uses Postgres as its backend database.<\/p>\n<p>Here is what you would need to get PHP to work with Postgres:<br \/>\na. Apache<br \/>\nb. PHP with pgsql option<br \/>\nc. <a href=\"http:\/\/www.postgresql.org\/download\/\" target=\"_blank\">Postgres <\/a><\/p>\n<p>If you don&#8217;t want to install each of the component by yourself then you can optionally use the <a href=\"http:\/\/bitnami.org\/stacks\" target=\"_blank\">stack from bitnami.org<\/a>\u00c2\u00a0Install the appropriate stack for your operating system. If you are using Ubuntu (debian), you can do\u00c2\u00a0apt-get install php5-pgsql.<\/p>\n<p>Open up the php.ini file and make sure you have the PostgresSQL extensions turned on. \u00c2\u00a0If you are on Linux the install\/compile PHP5 with pgsql . With windows you may have to install the pgsql extensions.<\/p>\n<p>Lets get into postgres and create a database , table and insert some data that we can query using php.<\/p>\n<p>Assuming the userid you are on has \u00c2\u00a0postgres libraries in its environments, open a command prompt.<br \/>\n<strong>Create a Database :<\/strong><br \/>\n<span style=\"color: #ff9900;\">postgres@ubuntu:\/home\/public_html$ createdb rajudb<br \/>\n<\/span><br \/>\n<strong>Get to a psql prompt :<\/strong><br \/>\n<span style=\"color: #ff9900;\">postgres@ubuntu:\/home\/public_html$ psql<\/span><\/p>\n<p><span style=\"color: #99cc00;\"> psql (8.4.11)<\/span><br \/>\n<span style=\"color: #99cc00;\"> Type &#8220;help&#8221; for help.<\/p>\n<p><\/span><strong>Connect to database rajudb:<\/strong><br \/>\n<span style=\"color: #ff9900;\">postgres=# \\c rajudb<\/span><\/p>\n<p><span style=\"color: #99cc00;\">psql (8.4.11)<\/span><br \/>\n<span style=\"color: #99cc00;\"> You are now connected to database &#8220;rajudb&#8221;.<br \/>\n<\/span><br \/>\n<strong>Create a table called emailaddress:<\/strong><br \/>\n<span style=\"color: #ff9900;\">rajudb=# create table emailaddress(userid char(4) not null,emailid char(25), primary key(userid));<\/span><br \/>\n<span style=\"color: #99cc00;\">NOTICE:\u00c2\u00a0 CREATE TABLE \/ PRIMARY KEY will create implicit index &#8220;emailaddress_pkey&#8221; for table &#8220;emailaddress&#8221;<\/span><br \/>\n<span style=\"color: #99cc00;\"> CREATE TABLE<\/span><\/p>\n<p><strong>Insert data into the table emailaddress that you created earlier:<\/strong><br \/>\n<span style=\"color: #ff9900;\">rajudb=# insert into emailaddress values(&#8216;A001&#8242;,&#8217;a001@techsatwork.com&#8217;);<\/span><br \/>\n<span style=\"color: #99cc00;\"> INSERT 0 1<\/span><br \/>\n<span style=\"color: #ff9900;\"> rajudb=# insert into emailaddress values(&#8216;A002&#8242;,&#8217;a002@techsatwork.com&#8217;);<\/span><br \/>\n<span style=\"color: #99cc00;\"> INSERT 0 1<\/span><br \/>\n<span style=\"color: #ff9900;\"> rajudb=# insert into emailaddress values(&#8216;A003&#8242;,&#8217;a003@techsatwork.com&#8217;);<\/span><br \/>\n<span style=\"color: #99cc00;\"> INSERT 0 1<\/span><\/p>\n<p><strong>Select data from the table :<\/strong><br \/>\n<span style=\"color: #ff9900;\">rajudb=# select * from emailaddress;<\/span><br \/>\n<span style=\"color: #99cc00;\">userid |\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 emailid<\/span><br \/>\n<span style=\"color: #99cc00;\"> &#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/span><br \/>\n<span style=\"color: #99cc00;\"> A001\u00c2\u00a0\u00c2\u00a0 | a001@techsatwork.com<\/span><br \/>\n<span style=\"color: #99cc00;\"> A002\u00c2\u00a0\u00c2\u00a0 | a002@techsatwork.com<\/span><br \/>\n<span style=\"color: #99cc00;\"> A003\u00c2\u00a0\u00c2\u00a0 | a003@techsatwork.com<\/span><br \/>\n<span style=\"color: #99cc00;\"> (3 rows)<\/span><\/p>\n<p>Now lets try to retrieve the same rows using a web browser through a php script. Make sure your apache webserver is running and php is enabled to connect to postgres. Open a text editor and type the following and save it on the web server directory .<\/p>\n<p><span style=\"color: #ff9900;\">&lt;!DOCTYPE html PUBLIC &#8220;-\/\/W3C\/\/DTD HTML 4.01\/\/EN&#8221;&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\"> &lt;html&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\"> &lt;head&gt;&lt;\/head&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\"> &lt;body&gt;<\/span><\/p>\n<p><span style=\"color: #ff9900;\">&lt;?php<\/span><br \/>\n<span style=\"color: #ff9900;\"> \/\/ attempt a connection<\/span><br \/>\n<span style=\"color: #ff9900;\"> $dbh = pg_connect(&#8220;host=localhost dbname=rajudb user=postgres password=secret!&#8221;);<\/span><br \/>\n<span style=\"color: #ff9900;\"> if (!$dbh) {<\/span><br \/>\n<span style=\"color: #ff9900;\"> die(&#8220;Error in connection: &#8221; . pg_last_error());<\/span><br \/>\n<span style=\"color: #ff9900;\"> }<\/span><\/p>\n<p><span style=\"color: #ff9900;\">\/\/ execute query<\/span><br \/>\n<span style=\"color: #ff9900;\"> $sql = &#8220;SELECT * FROM emailaddress&#8221;;<\/span><br \/>\n<span style=\"color: #ff9900;\"> $result = pg_query($dbh, $sql);<\/span><br \/>\n<span style=\"color: #ff9900;\"> if (!$result) {<\/span><br \/>\n<span style=\"color: #ff9900;\"> die(&#8220;Error in SQL query: &#8221; . pg_last_error());<\/span><br \/>\n<span style=\"color: #ff9900;\"> }<\/span><\/p>\n<p><span style=\"color: #ff9900;\">\/\/ iterate over result set<\/span><br \/>\n<span style=\"color: #ff9900;\"> \/\/ print each row<\/span><br \/>\n<span style=\"color: #ff9900;\"> while ($row = pg_fetch_array($result)) {<\/span><br \/>\n<span style=\"color: #ff9900;\"> echo &#8220;User Id: &#8221; . $row[0] . &#8220;&lt;br \/&gt;&#8221;;<\/span><br \/>\n<span style=\"color: #ff9900;\"> echo &#8220;Email address: &#8221; . $row[1] . &#8220;&lt;p \/&gt;&#8221;;<\/span><br \/>\n<span style=\"color: #ff9900;\"> }<\/span><\/p>\n<p><span style=\"color: #ff9900;\">\/\/ free memory<\/span><br \/>\n<span style=\"color: #ff9900;\"> pg_free_result($result);<\/span><\/p>\n<p><span style=\"color: #ff9900;\">\/\/ close connection<\/span><br \/>\n<span style=\"color: #ff9900;\"> pg_close($dbh);<\/span><br \/>\n<span style=\"color: #ff9900;\"> ?&gt;<\/span><\/p>\n<p><span style=\"color: #ff9900;\">&lt;\/body&gt;<\/span><br \/>\n<span style=\"color: #ff9900;\"> &lt;\/html&gt;<\/span><\/p>\n<p><span style=\"color: #ff9900;\">\u00c2\u00a0<span style=\"color: #000000;\">This is one set of pg functions that you can use to interact with postgres using php. To find out about others, refer to the <a href=\"http:\/\/php.net\/manual\/en\/book.pgsql.php\" target=\"_blank\">php manuals.<\/a>\u00c2\u00a0<\/span><\/span><\/p>\n<p><span style=\"color: #000000;\"><a href=\"http:\/\/bitnami.org\/stack\/wappstack\" target=\"_blank\"><span style=\"color: #000000;\"><br \/>\n<\/span><\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been using PHP for a while now. \u00c2\u00a0When people say PHP, its usually associated with \u00c2\u00a0 \u00c2\u00a0 MySQL as its backend database. But PHP will work with pretty much any database. \u00c2\u00a0Few years ago I wrote about how to get PHP to work with DB2. \u00c2\u00a0 Here I am going to attempt to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[53,303,3,358],"tags":[830,828,943,833,834,56,829,831,827,832],"class_list":["post-1337","post","type-post","status-publish","format-standard","hentry","category-applications","category-database","category-db2","category-how-to","tag-bitmani","tag-connect-php-to-postgres","tag-db2","tag-pg_connect","tag-pg_query","tag-php","tag-php-and-postgres","tag-php5-pgsql","tag-postgres","tag-psql"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1337"}],"version-history":[{"count":5,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1337\/revisions"}],"predecessor-version":[{"id":1343,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1337\/revisions\/1343"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}