What is PostgreSQL?
PostgreSQL is a powerful, open source object-relational database system ( ORDBMS ). Unlike other relational database systems, PostgreSQL (Postgres) allows users to create unique operators, complex data types, aggregate functions, data type conversion character, and other various database objects through the SQL function.
In this article, we are going to show you how to install PostgreSQL 11 database on Ubuntu 18.04 LTS or Ubuntu 16.04 LTS.
Step 1: Add PostgreSQL Apt Repository
– Add PostgreSQL apt repository
# sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main" > /etc/apt/sources.list.d/PostgreSQL.list'
– Import the repository signing key, and update the package lists
# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Step 2: Install PostgreSQL
# sudo apt-get update # sudo apt-get install postgresql-11
– The below commands can be used to stop, start, enable and check its status
# sudo systemctl stop postgresql.service # sudo systemctl start postgresql.service # sudo systemctl enable postgresql.service # sudo systemctl status postgresql.service
Step 3: Connect to PostgreSQL
– After installing PostgreSQL 11 database server, by default PostgreSQL, will create a system account user named postgres with role postgres.
– To connect to the PostgreSQL database server, use the following commands:
~# su -l postgres ~$ psql psql (11.1 (Ubuntu 11.1-1.pgdg18.04+1)) Type "help" for help. postgres=#
Step 4: Usage Examples
– To check log info use the below comand:
postgres=# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
– To change the password of postgres role you can use:
postgres=# \password postgres
– To list all the databaes on your PostgreSQL database server just run one of the following commands:
postgres=# \list postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)
– To connect to a specific database on your PostgreSQL database server you can the below command \c
followed by the name of your database:
postgres=# \c postgres
You are now connected to database “postgres” as user “postgres”.
– List all the tables after have been connected to a specific database
postgres=# \d
– To create a new database on your PostgreSQL database server you can use one the following commands followed by the database name :
postgres=# createdb database_name postgres=# createdb database_name OWNER rolename;
– To Create a table use the following command:
postgres=# create table employees (name varchar(25), surname varchar(25));
– To insert records into a specific tables, use the INSERT query as below:
postgres=# INSERT INTO employees VALUES ('Lotfi','waderni');
– Leave the psql command line
postgres=# \q
Conclusion
You have successfully installed the PostgreSQL 11 database server on Ubuntu 16.04 LTS and Ubuntu 18.04 LTS system. Here are some more guides that cover how to use PostgreSQL (Postgres):
- How To Enable Network Remote Access To PostgreSQL Database Server
- how to install PgAdmin 4 in Server Mode as a web application using apache2 and Wsgi module on Ubuntu 16.04 LTS and Ubuntu 18.04 LTS
We hope this tutorial was enough Helpful. If you need more information, or have any questions, just comment below and we will be glad to assist you!
1 comment
You may want to explain how to actually make this work, though:
“`
$ sudo apt-get install postgresql-client-11
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package postgresql-client-11
“`