{"id":1930,"date":"2016-10-28T17:15:06","date_gmt":"2016-10-28T14:15:06","guid":{"rendered":"http:\/\/www.systemcodegeeks.com\/?p=1930"},"modified":"2017-11-30T16:04:16","modified_gmt":"2017-11-30T14:04:16","slug":"connect-postgresql-using-php","status":"publish","type":"post","link":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/","title":{"rendered":"Connect to PostgreSQL using PHP"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.systemcodegeeks.com\/databases\/postgresql\/postgresql-database-tutorial\/\">PostgreSQL Database Tutorial<\/a>.<\/p>\n<p>In this course, we provide a compilation of PostgreSQL tutorials that will help you set up and run your own database management system. We cover a wide range of topics, from installation and configuration, to custom commands and datatypes. With our straightforward tutorials, you will be able to get your own projects up and running in minimum time. Check it out <a href=\"http:\/\/www.systemcodegeeks.com\/databases\/postgresql\/postgresql-database-tutorial\/\">here<\/a>!<\/em><\/p>\n<p>After we have learned how to set up and configure PostgreSQL for a variety of scenarios, having a database and populating it with data will not be of any use until we can retrieve it and use it in some way. Today, using a mobile-friendly web application is the most common way to accomplish this goal.<\/p>\n<p>In this tutorial we will explain how to connect to our PostgreSQL database server using PHP, a popular server-side scripting language, how to retrieve data, and how to display it in a web page. Using this foundation, you will be able to go on to create more robust applications that make use of PostgreSQL and PHP.<br \/>\n[ulp id=&#8217;mezv5fambxsNcQw8&#8242;]<\/p>\n<h2>1. Installing the software<\/h2>\n<p>As we just mentioned, we will use PHP to connect to the database server and to display the results of a query in a web page. Before we even start writing the application, we will need to install PHP and some additional packages &#8211; including the Apache web server. To do this in an Ubuntu 16.04 server with IP 192.168.0.54, use the following command:<\/p>\n<pre class=\"brush:bash\">sudo aptitude update &amp;&amp; sudo aptitude install apache2 postgresql-contrib php7.0-pgsql<\/pre>\n<p>After the installation is complete, create a php file named info.php under \/var\/www\/html with the following three lines. This will help us to verify that PHP has been installed along with the PostgreSQL dependencies:<\/p>\n<pre class=\"brush:php\">&lt;?php\r\nphpinfo();\r\n?&gt;<\/pre>\n<p>Then browse to 192.168.0.54\/info.php and look for the section with the PostgreSQL details. You should find that the PDO driver is enabled and that PHP is supporting our RDBMS, as shown in Fig. 1:<\/p>\n<figure id=\"attachment_1933\" aria-describedby=\"caption-attachment-1933\" style=\"width: 607px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/01php.png\"><img decoding=\"async\" class=\" wp-image-1933\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/01php.png\" alt=\"Checking the status of PostgreSQL-related PHP components\" width=\"607\" height=\"327\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/01php.png 794w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/01php-300x162.png 300w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/01php-768x414.png 768w\" sizes=\"(max-width: 607px) 100vw, 607px\" \/><\/a><figcaption id=\"caption-attachment-1933\" class=\"wp-caption-text\">Figure 1 &#8211; Checking the status of PostgreSQL-related PHP components<\/figcaption><\/figure>\n<p>Now we\u2019re ready to start writing our simple, yet functional PHP-based application.<\/p>\n<h2>2. Connecting to the database server<\/h2>\n<p>The first thing that we must do is ensure PHP can connect to the database server. Create a file named con.php under \/var\/www\/html with the following contents:<\/p>\n<pre class=\"brush:php\">&lt;?php\r\n\r\n\/\/ Connection details\r\n$conn_string = \"host=localhost port=5432 dbname=World_db user=scg password=MyPassword options='--client_encoding=UTF8'\";\r\n\r\n\/\/ Establish a connection with MySQL server\r\n$dbconn = pg_connect($conn_string);\r\n\r\n\/\/ Check connection status. Exit in case of errors\r\nif(!$dbconn) {\r\necho \"Error: Unable to open database\\n\";\r\n} else {\r\necho \"Opened database successfully\\n\";\r\n}\r\n\r\n\/\/ Close connection\r\npg_close($dbconn);\r\n\r\n?&gt;<\/pre>\n<p>For security purposes, set the appropriate ownership to the Linux account postgres (the user the database service runs as) and add the www-data account to the postgres group. This will allow Apache to read this file:<\/p>\n<pre class=\"brush:bash\">sudo chown postgres:postgres \/var\/www\/html\/con.php\r\nsudo chmod 660 \/var\/www\/html\/con.php\r\nsudo usermod -a -G postgres www-data<\/pre>\n<p>Now go to 192.168.0.54\/con.php and make sure the connection to the database is successful before proceeding:<\/p>\n<figure id=\"attachment_1934\" aria-describedby=\"caption-attachment-1934\" style=\"width: 349px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/02php.png\"><img decoding=\"async\" class=\"size-full wp-image-1934\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/02php.png\" alt=\"Verifying database connection via PHP\" width=\"349\" height=\"159\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/02php.png 349w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/02php-300x137.png 300w\" sizes=\"(max-width: 349px) 100vw, 349px\" \/><\/a><figcaption id=\"caption-attachment-1934\" class=\"wp-caption-text\">Figure 2 &#8211; Verifying database connection via PHP<\/figcaption><\/figure>\n<p>If you get a blank page instead of the confirmation message shown in Fig. 2, inspect the Apache logs to troubleshoot. A missing semicolon or a misplaced quote can cause the connection to fail.<\/p>\n<h2>3. Writing the application<\/h2>\n<p>To begin, we will comment out the following line in con.php:<\/p>\n<pre class=\"brush:php\">echo \"Opened database successfully\\n\";<\/pre>\n<p>and insert the following lines below it. Please note that we will use a very simple query that will retrieve city names and the district it belongs to in Argentina (you will later be able to change it to a more complicated query using Common Table Expressions, for example):<\/p>\n<pre class=\"brush:bash\">$query = \"SELECT name, district FROM city WHERE countrycode='ARG'\";\r\n$cities = pg_query($query) or die('Query failed: ' . pg_last_error());\r\n$myarray = array();\r\nwhile ($row = pg_fetch_assoc($cities)) {\r\n$myarray[] = $row;\r\n}\r\n\r\n\/\/ Encode response into JSON array\r\necho json_encode($myarray);<\/pre>\n<p>The con.php file should now look as seen in Fig. 3:<\/p>\n<figure id=\"attachment_1935\" aria-describedby=\"caption-attachment-1935\" style=\"width: 700px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/03php.png\"><img decoding=\"async\" class=\"size-full wp-image-1935\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/03php.png\" alt=\"The connection file\" width=\"700\" height=\"414\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/03php.png 700w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/03php-300x177.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><figcaption id=\"caption-attachment-1935\" class=\"wp-caption-text\">Figure 3 &#8211; The connection file<\/figcaption><\/figure>\n<p>Save the changes and grant privileges on the city table for the scg user. Note that you\u2019ll have to do this from the PostgreSQL prompt:<\/p>\n<pre class=\"brush:sql\">GRANT ALL PRIVILEGES ON TABLE city TO scg;<\/pre>\n<p>Next, go to 192.168.0.54\/con.php. You should see the results of the query in JSON format (see Fig. 4):<\/p>\n<figure id=\"attachment_1936\" aria-describedby=\"caption-attachment-1936\" style=\"width: 532px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/04php.png\"><img decoding=\"async\" class=\" wp-image-1936\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/04php.png\" alt=\"Query results in JSON format\" width=\"532\" height=\"339\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/04php.png 661w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/04php-300x191.png 300w\" sizes=\"(max-width: 532px) 100vw, 532px\" \/><\/a><figcaption id=\"caption-attachment-1936\" class=\"wp-caption-text\">Figure 4 &#8211; Query results in JSON format<\/figcaption><\/figure>\n<div class=\"tip\">JSON stands for <i>JavaScript Object Notation<\/i>. It is a lightweight data-interchange format that is easy for humans to read and write and for machines to parse and generate.<\/div>\n<p>Now that we have successfully 1) connected to the database server, and 2) retrieved records into a JSON array, we will explain how to display this information into a mobile-friendly web page.<\/p>\n<h2>4. Creating a mobile-friendly web page<\/h2>\n<p>Most web developers nowadays use a robust HTML5\/CSS\/Javascript framework called Bootstrap to write mobile-friendly applications very easily. Though a full discussion about Bootstrap (and the HTML5-related technologies) is out of the scope of this article, it is sufficient to say that one of its distinguishing characteristics is that it divides the viewport in a 12-column grid.<\/p>\n<p>It is up to the developer to decide how many columns will be assigned to a particular piece of content for xs (extra small, i.e. cell phones), sm (small, i.e. tablets and ipads), md (medium, i.e. laptops), and lg or large devices (high resolution monitors). In this tutorial we will assume that we desire to show the city and district fields using 6 columns each in small devices (sm) and up. For extra small screens, city will stack on top of district, as we will see later.<\/p>\n<p>To do this, create a file named index.php in the same location as con.php and insert the following lines into it:<\/p>\n<pre class=\"brush:php\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=\"utf-8\"&gt;\r\n&lt;title&gt;Mobile friendly page with PostgreSQL and PHP&lt;\/title&gt;\r\n&lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.7\/css\/bootstrap.min.css\"&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div class=\"container\"&gt;\r\n&lt;div class=\"row\"&gt;\r\n&lt;div class=\"col-md-6\" id=\"city\" style=\"text-align: center\"&gt;&lt;strong&gt;City&lt;\/strong&gt;&lt;\/div&gt;\r\n&lt;div class=\"col-md-6\" id=\"district\" style=\"text-align: center\"&gt;&lt;strong&gt;District&lt;\/strong&gt;&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.1.1.min.js\"&lt;\/script&gt;\r\n&lt;script src=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.7\/js\/bootstrap.min.js\"&lt;\/script&gt;\r\n&lt;\/html&gt;\r\n&lt;script&gt;\r\n$(document).ready(function(){\r\n$.ajax({\r\nurl: 'con.php',\r\ndatatype: 'json',\r\ntype: 'POST',\r\nsuccess: function(data){\r\nvar output = $.parseJSON(data);\r\nfor(var i =0;i &lt; output.length;i++)\r\n{\r\nvar item = output[i];\r\n$(\"#city\").append(\"&lt;br&gt;\"+item.name);\r\n$(\"#district\").append(\"&lt;br&gt;\"+item.district);\r\n}\r\n}}\r\n);\r\n});\r\n&lt;\/script&gt;<\/pre>\n<p>As you can see, this simple page uses a well-known Javascript library called jQuery to make an Ajax call to con.php and retrieve the results. Again, an adequate discussion about jQuery, Ajax, and Javascript is out of the scope of this article, but you can find some very valuable information on <a href=\"http:\/\/www.w3schools.com\/jquery\/\">W3schools<\/a>.<\/p>\n<p>When you browse to 192.168.0.54\/index.php, the result should be similar to Fig. 5:<\/p>\n<figure id=\"attachment_1937\" aria-describedby=\"caption-attachment-1937\" style=\"width: 802px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/05php.png\"><img decoding=\"async\" class=\"size-full wp-image-1937\" src=\"http:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/05php.png\" alt=\"Displaying the web page with the results of the query\" width=\"802\" height=\"391\" srcset=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/05php.png 802w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/05php-300x146.png 300w, https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/10\/05php-768x374.png 768w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/a><figcaption id=\"caption-attachment-1937\" class=\"wp-caption-text\">Figure 5 &#8211; Displaying the web page with the results of the query<\/figcaption><\/figure>\n<p>Feel free to resize your browser\u2019s window to see the visualization changes as the viewport changes.<\/p>\n<h2>5. Summary<\/h2>\n<p>If you followed this tutorial carefully, congratulations! You have set connected to your PostgreSQL server using PHP and displayed data from your database in a mobile-friendly web page. Hopefully this will give you the foundation to create more sophisticated applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled PostgreSQL Database Tutorial. In this course, we provide a compilation of PostgreSQL tutorials that will help you set up and run your own database management system. We cover a wide range of topics, from installation and configuration, to custom commands and datatypes. With our straightforward tutorials, &hellip;<\/p>\n","protected":false},"author":15,"featured_media":198,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1930","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Connect to PostgreSQL using PHP - System Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled PostgreSQL Database Tutorial. In this course, we provide a compilation of PostgreSQL tutorials that will\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Connect to PostgreSQL using PHP - System Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled PostgreSQL Database Tutorial. In this course, we provide a compilation of PostgreSQL tutorials that will\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"System Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/systemcodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/gacanepa\" \/>\n<meta property=\"article:published_time\" content=\"2016-10-28T14:15:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-30T14:04:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/postgresql-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Gabriel Canepa\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@gacanepa\" \/>\n<meta name=\"twitter:site\" content=\"@systemcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gabriel Canepa\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/\"},\"author\":{\"name\":\"Gabriel Canepa\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/967da353d0f1a1de21c9504942625a5f\"},\"headline\":\"Connect to PostgreSQL using PHP\",\"datePublished\":\"2016-10-28T14:15:06+00:00\",\"dateModified\":\"2017-11-30T14:04:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/\"},\"wordCount\":1018,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/postgresql-logo.jpg\",\"articleSection\":[\"PostgreSQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/\",\"url\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/\",\"name\":\"Connect to PostgreSQL using PHP - System Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/postgresql-logo.jpg\",\"datePublished\":\"2016-10-28T14:15:06+00:00\",\"dateModified\":\"2017-11-30T14:04:16+00:00\",\"description\":\"This article is part of our Academy Course titled PostgreSQL Database Tutorial. In this course, we provide a compilation of PostgreSQL tutorials that will\",\"breadcrumb\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#primaryimage\",\"url\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/postgresql-logo.jpg\",\"contentUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/postgresql-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.systemcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Databases\",\"item\":\"https:\/\/www.systemcodegeeks.com\/category\/databases\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL\",\"item\":\"https:\/\/www.systemcodegeeks.com\/category\/databases\/postgresql\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Connect to PostgreSQL using PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#website\",\"url\":\"https:\/\/www.systemcodegeeks.com\/\",\"name\":\"System Code Geeks\",\"description\":\"Operating System Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.systemcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.systemcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/systemcodegeeks\",\"https:\/\/x.com\/systemcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/967da353d0f1a1de21c9504942625a5f\",\"name\":\"Gabriel Canepa\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/27b3ea2a3fb1de4ed1c8694a1465c099a86586d8b833a0d852a26d76d750df9f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/27b3ea2a3fb1de4ed1c8694a1465c099a86586d8b833a0d852a26d76d750df9f?s=96&d=mm&r=g\",\"caption\":\"Gabriel Canepa\"},\"description\":\"Gabriel Canepa is a Linux Foundation Certified System Administrator (LFCS-1500-0576-0100) and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work. When he's not typing commands or writing code or articles, he enjoys telling bedtime stories with his wife to his two little daughters and playing with them, the great pleasure of his life.\",\"sameAs\":[\"http:\/\/www.gabrielcanepa.com.ar\/\",\"https:\/\/www.facebook.com\/gacanepa\",\"https:\/\/ar.linkedin.com\/in\/gacanepa\",\"https:\/\/x.com\/gacanepa\"],\"url\":\"https:\/\/www.systemcodegeeks.com\/author\/gabriel-canepa\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Connect to PostgreSQL using PHP - System Code Geeks - 2026","description":"This article is part of our Academy Course titled PostgreSQL Database Tutorial. In this course, we provide a compilation of PostgreSQL tutorials that will","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/","og_locale":"en_US","og_type":"article","og_title":"Connect to PostgreSQL using PHP - System Code Geeks - 2026","og_description":"This article is part of our Academy Course titled PostgreSQL Database Tutorial. In this course, we provide a compilation of PostgreSQL tutorials that will","og_url":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/","og_site_name":"System Code Geeks","article_publisher":"https:\/\/www.facebook.com\/systemcodegeeks","article_author":"https:\/\/www.facebook.com\/gacanepa","article_published_time":"2016-10-28T14:15:06+00:00","article_modified_time":"2017-11-30T14:04:16+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/postgresql-logo.jpg","type":"image\/jpeg"}],"author":"Gabriel Canepa","twitter_card":"summary_large_image","twitter_creator":"@gacanepa","twitter_site":"@systemcodegeeks","twitter_misc":{"Written by":"Gabriel Canepa","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#article","isPartOf":{"@id":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/"},"author":{"name":"Gabriel Canepa","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/967da353d0f1a1de21c9504942625a5f"},"headline":"Connect to PostgreSQL using PHP","datePublished":"2016-10-28T14:15:06+00:00","dateModified":"2017-11-30T14:04:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/"},"wordCount":1018,"commentCount":0,"publisher":{"@id":"https:\/\/www.systemcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/postgresql-logo.jpg","articleSection":["PostgreSQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/","url":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/","name":"Connect to PostgreSQL using PHP - System Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.systemcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#primaryimage"},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/postgresql-logo.jpg","datePublished":"2016-10-28T14:15:06+00:00","dateModified":"2017-11-30T14:04:16+00:00","description":"This article is part of our Academy Course titled PostgreSQL Database Tutorial. In this course, we provide a compilation of PostgreSQL tutorials that will","breadcrumb":{"@id":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#primaryimage","url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/postgresql-logo.jpg","contentUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2016\/01\/postgresql-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.systemcodegeeks.com\/databases\/postgresql\/connect-postgresql-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.systemcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Databases","item":"https:\/\/www.systemcodegeeks.com\/category\/databases\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL","item":"https:\/\/www.systemcodegeeks.com\/category\/databases\/postgresql\/"},{"@type":"ListItem","position":4,"name":"Connect to PostgreSQL using PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.systemcodegeeks.com\/#website","url":"https:\/\/www.systemcodegeeks.com\/","name":"System Code Geeks","description":"Operating System Developers Resource Center","publisher":{"@id":"https:\/\/www.systemcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.systemcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.systemcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.systemcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.systemcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/systemcodegeeks","https:\/\/x.com\/systemcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/967da353d0f1a1de21c9504942625a5f","name":"Gabriel Canepa","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.systemcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/27b3ea2a3fb1de4ed1c8694a1465c099a86586d8b833a0d852a26d76d750df9f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/27b3ea2a3fb1de4ed1c8694a1465c099a86586d8b833a0d852a26d76d750df9f?s=96&d=mm&r=g","caption":"Gabriel Canepa"},"description":"Gabriel Canepa is a Linux Foundation Certified System Administrator (LFCS-1500-0576-0100) and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work. When he's not typing commands or writing code or articles, he enjoys telling bedtime stories with his wife to his two little daughters and playing with them, the great pleasure of his life.","sameAs":["http:\/\/www.gabrielcanepa.com.ar\/","https:\/\/www.facebook.com\/gacanepa","https:\/\/ar.linkedin.com\/in\/gacanepa","https:\/\/x.com\/gacanepa"],"url":"https:\/\/www.systemcodegeeks.com\/author\/gabriel-canepa\/"}]}},"_links":{"self":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts\/1930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/comments?post=1930"}],"version-history":[{"count":0,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/posts\/1930\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/media\/198"}],"wp:attachment":[{"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/media?parent=1930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/categories?post=1930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.systemcodegeeks.com\/wp-json\/wp\/v2\/tags?post=1930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}