{"id":784,"date":"2015-02-25T11:07:18","date_gmt":"2015-02-25T18:07:18","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=784"},"modified":"2015-02-25T11:07:18","modified_gmt":"2015-02-25T18:07:18","slug":"how-to-install-and-use-postgresql-on-ubuntu-12-04","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2015\/02\/25\/how-to-install-and-use-postgresql-on-ubuntu-12-04\/","title":{"rendered":"How To Install and Use PostgreSQL on Ubuntu 12.04"},"content":{"rendered":"<h2>Install Postgres<\/h2>\n<p>Before we install postgres, we should quick perform a quick update of the apt-get repository:<\/p>\n<pre>apt-get update<\/pre>\n<p>Once apt-get has updated go ahead and download Postgres and its helpful accompanying dependencies:<\/p>\n<pre>sudo apt-get install postgresql postgresql-contrib<\/pre>\n<p>With that, postgres is installed on your server.<\/p>\n<div data-unique=\"create-your-postgresql-roles-and-databases\"><\/div>\n<h2>Create Your PostgreSQL Roles and Databases<\/h2>\n<p>Once Postgres has been installed on your server, you can start to configure the database.<\/p>\n<p>Postgres uses the concept of roles to distinguish the variety of users that can connect to a database. When it is first installed on a server, the default postgres user is actual named \u201cpostgres\u201d. The other users are specified in one of variety of ways. The common methods are <i>ident<\/i> and <i>md5<\/i>. The postgres default is to use <i>ident<\/i> authentication, tying each server user to a Postgres account. The alternative which can be set in the authentication configuration, located in \u201c\/etc\/postgresql\/9.1\/main\/pg_hba.conf \u201c is md5 which asks the client to supply an encrypted password.<\/p>\n<p>To begin creating custom users, first switch into the default user:<\/p>\n<pre>sudo su \u2013 postgres<\/pre>\n<p>Once logged in as this user, you can move forward to create more roles in your PostgreSQL system:<\/p>\n<pre>createuser<\/pre>\n<pre>Enter name of role to add: newuser\nShall the new role be a superuser? (y\/n) y<\/pre>\n<p>To outfit your user with a password, you can add the words \u2013pwprompt to the createuser command:<\/p>\n<pre>createuser --pwprompt<\/pre>\n<div data-unique=\"connecting-to-the-postgres-databases\"><\/div>\n<h2>Connecting to the Postgres Databases<\/h2>\n<p>With the users that you want to use to log into your Postgres shell set up, you can proceed to make a database for them to use.<\/p>\n<p>You can create the Postgres database as a superuser. In this case, we will use the default super user.<\/p>\n<p>Go ahead and switch into the postgres user once again:<\/p>\n<pre>su \u2013 postgres<\/pre>\n<p>As postgres, you can start to create your first usable postgres database:<\/p>\n<pre> createdb events<\/pre>\n<p>And with that you can finally connect to the postgres shell.<\/p>\n<div data-unique=\"how-to-create-and-delete-a-postgres-tables\"><\/div>\n<h2>How to Create and Delete a Postgres Tables<\/h2>\n<p>Once we log into the correct database (using the <code>psql -d events<\/code> command where <strong>events<\/strong> is that database&#8217;s name), we can create tables within it.<\/p>\n<p>Let\u2019s imagine that we are planning a get together of friends. We can use Postgres to track the details of the event.<\/p>\n<p>Let\u2019s create a new Postgres table:<\/p>\n<pre>CREATE TABLE potluck (name VARCHAR(20),\nfood VARCHAR(30),\nconfirmed CHAR(1), \nsignup_date DATE);<\/pre>\n<p>This command accomplishes a number of things:<\/p>\n<ol>\n<li>It has created a table called potluck within the database, newdb.<\/li>\n<li>We have set up 4 columns in the table\u2014name, food, confirmed, and signup date.<\/li>\n<li>The \u201cname\u201d column has been limited by the VARCHAR command to be under 20 characters long.<\/li>\n<li>The \u201cfood\u201d column designates the food each person will bring. The VARCHAR limits text to be under 30 characters.<\/li>\n<li>The \u201cconfirmed\u201d column records whether the person has RSVP\u2019d with one letter, Y or N.<\/li>\n<li>The \u201cdate\u201d column will show when they signed up for the event. Postgres requires that dates be written as yyyy-mm-dd<\/li>\n<\/ol>\n<p>Once entered, postgres should confirm the table creation with the following line:<\/p>\n<pre>CREATE TABLE<\/pre>\n<p>You can additionally see all of the tables within the database with the following command:<\/p>\n<pre>\\dt<\/pre>\n<p>The result, in this case, should look like this:<\/p>\n<pre>postgres=# \\dt\n        List of relations\n Schema |  Name   | Type  | Owner \n--------+---------+-------+-------\n public | potluck | table | root\n(1 row)<\/pre>\n<div data-unique=\"-how-to-add-information-to-a-postgres-table\"><\/div>\n<h2>How to Add Information to a Postgres Table<\/h2>\n<p>We have a working table for our party. Now it\u2019s time to start filling in the details.<\/p>\n<p>Use this format to insert information into each row:<\/p>\n<pre>INSERT INTO potluck (name, food, confirmed, signup_date) VALUES('John', 'Casserole', 'Y', '2012-04-11');<\/pre>\n<p>Once you input that in, you will see the words:<\/p>\n<pre>INSERT 0 1<\/pre>\n<p>Let\u2019s add a couple more people to our group:<\/p>\n<pre>INSERT INTO potluck (name, food, confirmed, signup_date) VALUES('Sandy', 'Key Lime Tarts', 'N', '2012-04-14');\nINSERT INTO potluck (name, food, confirmed, signup_date)VALUES ('Tom', 'BBQ','Y', '2012-04-18');\nINSERT INTO potluck (name, food, confirmed, signup_date) VALUES('Tina', 'Salad', 'Y','2012-04-18');<\/pre>\n<p>We can take a look at our table:<\/p>\n<pre>SELECT * FROM potluck;\n name  |      food      | confirmed | signup_date \n-------+----------------+-----------+-------------\n John  | Casserole      | Y         | 2012-04-11\n Sandy | Key Lime Tarts | N         | 2012-04-14\n Tom   | BBQ            | Y         | 2012-04-10\n Tina  | Salad          | Y         | 2012-04-18\n(4 rows)<\/pre>\n<p>Should we want to, then, follow up by removing an unlucky attendee, in this John and his casserole, from our potluck we can accomplish this with the Delete command:<\/p>\n<pre> DELETE FROM potluck WHERE name = 'John' ;<\/pre>\n<div data-unique=\"how-to-add-and-delete-a-column\"><\/div>\n<h2>How to Add and Delete a Column<\/h2>\n<p>We are creating a handy chart, but it is missing some important information: our attendees\u2019 emails.<\/p>\n<p>We can easily add this:<\/p>\n<pre>ALTER TABLE potluck ADD email VARCHAR(40);<\/pre>\n<p>This command puts the new column called &#8220;email&#8221; at the end of the table by default, and the VARCHAR command limits it to 40 characters.<\/p>\n<p>Just as you can add a column, you can delete one as well:<\/p>\n<pre>ALTER TABLE potluck DROP email;<\/pre>\n<p>I guess we will never know how to reach the picnickers.<\/p>\n<div data-unique=\"how-to-update-information-in-the-table\"><\/div>\n<h2>How to Update Information in the Table<\/h2>\n<p>Now that we have started our potluck list, we can address any possible changes.<\/p>\n<p>For example: Sandy has confirmed that she is attending, so we are going to update that in the table.<\/p>\n<pre> UPDATE potluck set confirmed = 'Y' WHERE name = 'Sandy';<\/pre>\n<p>You can also use this command to add information into specific cells, even if they are empty.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Install Postgres Before we install postgres, we should quick perform a quick update of the apt-get repository: apt-get update Once apt-get has updated go ahead&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-784","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=784"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/784\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}