How to install and use PostgreSQL on Ubuntu 18.04
PostgreSQL or Postgres is a relational database management system, provides an implementation of SQL query language. It is a popular choice for many small and large projects, and has the advantage of standards-compliant and has many advanced features (such as reliable transactions and concurrent read lock) of.
This guide shows how to install Postgres on 18.04 VPS instance Ubuntu, and provides instructions for basic database management.
prerequisites
In order to follow this tutorial, you will need an Ubuntu 18.04 server that has been configured by following our initial Ubuntu 18.04 server setup guide. After you complete this prerequisite tutorial, your server should have a non-root user with sudo privileges and basic firewall.
Installing PostgreSQL
The default Ubuntu software package repository contains Postgres, so you can use the apt
package system to install these packages.
Because this is the first time apt
in this session, please refresh the local package index. Then, install Postgres package and -contrib
add some additional utilities and functions of the software package:
sudo apt update
sudo apt install postgresql postgresql-contrib
Now install the software, we can understand the difference it works, and similar database management system it may be possible to use with your office.
Use roles and PostgreSQL database
By default, Postgres using a concept called "role" to handle authentication and authorization. These are similar to common Unix-style accounts in some respects, but Postgres does not distinguish between users and groups, but tend to be more flexible term "role."
After installation, Postgres is set to use ident identity verification, which means it will match the Postgres roles and Unix / Linux system account. Unix / Linux user name if there is a role Postgres, then with the same name as the role login.
The installation process creates a user account named postgres, which is the default Postgres associated with the role. In order to use Postgres, you can log in to that account.
There are several ways you can use this account to access Postgres.
Switch to the postgres account
Enter the following switch to the postgres account on the server:
sudo -i -u postgres
You can now access Postgres immediately prompt by typing the following command:
psql
This will take you log on to the PostgreSQL prompt, from here you can interact with the database management system immediately.
Enter the following command to exit the PostgreSQL prompt:
\q
This takes you back to the postgres
Linux command prompt.
Postgres prompt access without switching accounts
You can also directly run the command you want to use postgres account sudo
.
For example, in the last example, you are indicated by first switching to postgres user then runs psql
to open the Postgres Postgres prompt prompt. You can psql
as postgres user sudo
to complete this operation as a single operation command as follows:
sudo -u postgres psql
This will direct you to log on to Postgres, without the need for intermediate bash
shell.
Similarly, you can type the following command to exit the interactive Postgres session:
\q
Many use cases require multiple Postgres roles. Read on to learn how to configure them.
Create a new role
Currently, you only need to configure postgres role in the database. You can use the command line command to create a new role createrole
. The --interactive
flag will prompt you to enter the name of the new role, and asks whether it should have superuser privileges.
If you are logged postgres account, you can create new users by typing the following:
createuser --interactive
Conversely, if you prefer to use sudo
each command without switching from the general account, type:
sudo -u postgres createuser --interactive
The script prompts you some options, and perform the correct Postgres command to create the user according to your specifications based on your response.
Output
Enter name of role to add: sammyShall the new role be a superuser? (y/n) y
You get more control by passing some additional signs. By looking at man
the page viewing options:
man createuser
You now have a new installation of Postgres users, but you have not added any database. The next section describes this process.
Create a new database
Another assumption in the case of Postgres authentication system by default, for any role for login, this role will have the same name as the database that is accessible.
This means that if you create a user on a named sammy, the role will attempt to connect to the database by default, also known as "sammy" of. You can use this createdb
to create the appropriate database commands.
If you are logged postgres account, you can type something like this:
createdb sammy
Conversely, if you prefer to use sudo
without having to switch from general account each command, you can type:
sudo -u postgres createdb sammy
This flexibility offers a variety of paths to create the database as required
Postgres open to new roles tips
To use the ident
Log-based authentication, you need a database with the same name and Postgres role of Linux users.
If you do not match the available Linux user, you can use it adduser
to create a command. You must have from your non-root user account sudo
privileges (meaning not log in as the postgres user) to do this:
sudo adduser sammy
Once the new account is available, you can switch and connect to the database by typing the following command:
sudo -i -u sammy
psql
Or, you can do this inline:
sudo -u sammy psql
Assuming that all components are properly configured, this command will automatically log on.
If you want your users to connect to different databases, you can be achieved by specifying the database like this:
psql -d postgres</span
After logging in, you can enter the following to check the current connection information:
\conninfo
Output
You are connected to database "sammy" as user "sammy" via socket in "/var/run/postgresql" at port "5432".
If you connect to a non-default or non-default user database, which is useful.
Create and delete tables
Now that you know how to connect to PostgreSQL database system, you can learn some basic Postgres management tasks.
First, create a table to store some data. As an example, some of the tables described playground equipment.
The basic syntax of the command is as follows:
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, these commands provide a name for the table, and then define the maximum length of columns and column data types and fields. You can also choose to add a table constraint for each column.
You can in here to learn more about how to create and manage tables in Postgres the information.
For demonstration purposes, create a simple table that looks 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);
These commands create a playground equipment inventory form. It starts with a device ID serial
. This data type is an integer incremented automatically. You also gave the constraints of this column, primary key
which means that these values must be unique and not empty.
For two ( equip_id
and install_date
), these commands do not specify field length. This is because some types of columns necessary to provide length, because the length of the implicit type.
Equipment next two commands create columns type
and color
respectively, each of which may not be empty. These commands created after a location
column and create a constraint requiring one of eight possible values for this value. The last command creates a date column, the record date for your device.
You can enter the following to see the new table:
\d
Output
List of relations Schema | Name | Type | Owner --------+-------------------------+----------+------- public | playground | table | sammy public | playground_equip_id_seq | sequence | sammy(2 rows)
Your playground at the table here, but there are some known playground_equip_id_seq
something of this type sequence
. This is serial
your for the equip_id
representation of the type provided by the column. It keeps track of the next number in the sequence, and automatically created for this type of column.
If you want to see not only the form of a sequence, you can enter:
\dt
Output
List of relations Schema | Name | Type | Owner --------+------------+-------+------- public | playground | table | sammy(1 row)
In the table to add, delete, and query data
Now that you have a form, you can insert some data in it.
For example, by calling the table to be added to add slides and swings, column name, and provide data for each column, as follows:
INSERT INTO playground (type, color, location, install_date) VALUES ('slide', 'blue', 'south', '2017-04-28');
INSERT INTO playground (type, color, location, install_date) VALUES ('swing', 'yellow', 'northwest', '2018-08-16');
Input data should be careful to avoid several common hang up. For example, do not replace the column name in quotation marks, but the column values of the input required quotation marks.
Also to remember is that you do not enter equip_id
a value column. This is because whenever you create a new row in the table will automatically generate the data.
Retrieving information you add by entering the following:
SELECT * FROM playground;
Output
equip_id | type | color | location | install_date ----------+-------+--------+-----------+-------------- 1 | slide | blue | south | 2017-04-28 2 | swing | yellow | northwest | 2018-08-16(2 rows)
Here, you can see your equip_id
success and fill in all your organizations other data are correct.
If the slide on the playground broken and you have to remove it, you can also type the following to delete the row from the table:
DELETE FROM playground WHERE type = 'slide';
Inquiry Form again:
SELECT * FROM playground;
Output
equip_id | type | color | location | install_date ----------+-------+--------+-----------+-------------- 2 | swing | yellow | northwest | 2018-08-16(1 row)
You notice that your slides are no longer part of the table.
Add and remove columns from the table
After you create a form, it can be relatively easily modified to add or delete columns. Add one by entering the following to display the last time each piece of equipment maintenance access:
ALTER TABLE playground ADD last_maint date;
If you look at the table information again, you will see a new column has been added (but did not enter any data):
SELECT * FROM playground;
Output
equip_id | type | color | location | install_date | last_maint ----------+-------+--------+-----------+--------------+------------ 2 | swing | yellow | northwest | 2018-08-16 | (1 row)
Delete columns equally simple. If you find that the staff use a separate tool to track maintenance history, you can delete the column by typing the following:
ALTER TABLE playground DROP last_maint;
This will remove the last_maint
column and any value found, but leave all other data intact.
Update data in the table
So far, you've learned how to add records to the table and how to remove them, but not yet in this tutorial describes how to modify an existing entry.
You can set the value and you want to use by querying the records you want to update the column values for the existing entries. You can query the "wobble" record (which would match the table each swing) and change its color is "red." If you set up a paint job to swing, which may be useful:
UPDATE playground SET color = 'red' WHERE type = 'swing';
You can verify this by querying data again whether the operation was successful:
SELECT * FROM playground;
Output
equip_id | type | color | location | install_date ----------+-------+-------+-----------+-------------- 2 | swing | red | northwest | 2010-08-16(1 row)
As you can see, your slides are now registered as red.