Use PostgreSQL on CentOS 7

Create a New Role

From the postgres Linux account, you have the ability to log into the database system. However, we’re also going to demonstrate how to create additional roles. The postgres Linux account, being associated with the Postgres administrative role, has access to some utilities to create users and databases.
We can create a new role by typing:

  • createuser –interactive

This basically is an interactive shell script that calls the correct Postgres commands to create a user to your specifications. It will only ask you two questions: the name of the role and whether it should be a superuser. You can get more control by passing some additional flags. Check out the options by looking at the man page:

  • man createuser

Create a New Database

The way that Postgres is set up by default (authenticating roles that are requested by matching system accounts) also comes with the assumption that a matching database will exist for the role to connect to.
So if I have a user called test1, that role will attempt to connect to a database called test1 by default.
You can create the appropriate database by simply calling this command as the postgres user:

  • createdb test1

Connect to Postgres with the New User

Let’s assume that you have a Linux system account called test1 (you can create one by typing: sudo adduser test1), and that you have created a Postgres role and database also called test1.
You can change to the Linux system account by typing:

  • sudo -i -u test1

You can then connect to the test1 database as the test1 Postgres role by typing:

  • psql

This will log in automatically assuming that all of the components have been configured.
If you want your user to connect to a different database, you can do so by specifying the database like this (make sure you \q to the command prompt):

  • psql -d postgres

You can get information about the Postgres user you’re logged in as and the database you’re currently connected to by typing:

  • \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".

This can help remind you of your current settings if you are connecting to non-default databases or with non-default users.

Create and Delete Tables

Now that you know how to connect to the PostgreSQL database system, we will start to go over how to complete some basic tasks.
First, let’s create a table to store some data. Let’s create a table that describes playground equipment.
The basic syntax for this command is something like this:

  • CREATE TABLE table_name (
  • column_name1 col_type (field_length) column_constraints,
  • column_name2 col_type (field_length),
  • column_name3 col_type (field_length)
  • );

As you can see, we give the table a name, and then define the columns that we want, as well as the column type and the max length of the field data. We can also optionally add table constraints for each column.
You can learn more about how to create and manage tables in Postgres here.
For our purposes, we’re going to create a simple table like this:

  • CREATE TABLE playground (
  • equip_id serial PRIMARY KEY,
  • type varchar (50) NOT NULL,
  • color varchar (25) NOT NULL,
  • location varchar(25) check (location in (‘north’, ‘south’, ‘west’, ‘east’, ‘northeast’, ‘southeast’, ‘southwest’, ‘northwest’)),
  • install_date date
  • );

We have made a playground table that inventories the equipment that we have. This starts with an equipment ID, which is of the serial type. This data type is an auto-incrementing integer. We have given this column the constraint of primary key which means that the values must be unique and not null.
For two of our columns, we have not given a field length. This is because some column types don’t require a set length because the length is implied by the type.
We then give columns for the equipment type and color, each of which cannot be empty. We then create a location column and create a constraint that requires the value to be one of eight possible values. The last column is a date column that records the date that we installed the equipment.
We can see our new table by typing this:

  • \d
                   List of relations
 Schema |          Name           |   Type   |  Owner
 public | playground              | table    | postgres
 public | playground_equip_id_seq | sequence | postgres
(2 rows)

As you can see, we have our playground table, but we also have something called playground_equip_id_seq that is of the type sequence. This is a representation of the “serial” type we gave our equip_id column. This keeps track of the next number in the sequence.
If you want to see just the table, you can type:

  • \dt
           List of relations
 Schema |    Name    | Type  |  Owner
 public | playground | table | postgres
(1 row)

Add, Query, and Delete Data in a Table

Now that we have a table created, we can insert some data into it.
Let’s add a slide and a swing. We do this by calling the table we’re wanting to add to, naming the columns and then providing data for each column. Our slide and swing could be added like this:

  • INSERT INTO playground (type, color, location, install_date) VALUES (‘slide’, ‘blue’, ‘south’, ‘2014-04-28’);
  • INSERT INTO playground (type, color, location, install_date) VALUES (‘swing’, ‘yellow’, ‘northwest’, ‘2010-08-16’);

You should notice a few things. First, keep in mind that the column names should not be quoted, but the column values that you’re entering do need quotes.
Another thing to keep in mind is that we do not enter a value for the equip_id column. This is because this is auto-generated whenever a new row in the table is created.
We can then get back the information we’ve added by typing:

  • SELECT * FROM playground;
 equip_id | type  | color  | location  | install_date
        1 | slide | blue   | south     | 2014-04-28
        2 | swing | yellow | northwest | 2010-08-16
(2 rows)

Here, you can see that our equip_id has been filled in successfully and that all of our other data has been organized correctly.
If our slide breaks and we remove it from the playground, we can also remove the row from our table by typing:

  • DELETE FROM playground WHERE type = ‘slide’;

If we query our table again, we will see our slide is no longer a part of the table:

  • SELECT * FROM playground;
 equip_id | type  | color  | location  | install_date
        2 | swing | yellow | northwest | 2010-08-16
(1 row)

How To Add and Delete Columns from a Table

If we want to modify a table after it has been created to add an additional column, we can do that easily.
We can add a column to show the last maintenance visit for each piece of equipment by typing:

  • ALTER TABLE playground ADD last_maint date;

If you view your table information again, you will see the new column has been added (but no data has been entered):

  • SELECT * FROM playground;
 equip_id | type  | color  | location  | install_date | last_maint
        2 | swing | yellow | northwest | 2010-08-16   |
(1 row)

We can delete a column just as easily. If we find that our work crew uses a separate tool to keep track of maintenance history, we can get rid of the column here by typing:

  • ALTER TABLE playground DROP last_maint;

How To Update Data in a Table

We know how to add records to a table and how to delete them, but we haven’t covered how to modify existing entries yet.
You can update the values of an existing entry by querying for the record you want and setting the column to the value you wish to use. We can query for the “swing” record (this will match every swing in our table) and change its color to “red”. This could be useful if we gave it a paint job:

  • UPDATE playground SET color = ‘red’ WHERE type = ‘swing’;

We can verify that the operation was successful by querying our data again:

  • SELECT * FROM playground;
 equip_id | type  | color | location  | install_date
        2 | swing | red   | northwest | 2010-08-16
(1 row)

As you can see, our slide is now registered as being red.