How to install PostgreSQL on Ubuntu 16.04

Introduction

PostgreSQL is an object [relational database] (https://cloud.tencent.com/product/cdb-overview?from=10680) management system (ORDBMS) based on version 4.2 developed by the Department of Computer Science, University of California, Berkeley. PostgreSQL supports most of the SQL standards and provides many other modern features: complex queries, foreign keys, triggers, views, transaction integrity, MVCC. Similarly, PostgreSQL can be extended in many ways, for example, by adding new data types, functions, operators, aggregate functions, and indexes. Use, modify, and distribute PostgreSQL for free, whether for private, commercial, or academic research. The [PostgreSQL] (http://www.postgresql.org/) relational database system is a powerful, scalable, and standard open source database platform. This tutorial will help you install and configure PostgreSQL on the Tencent Cloud CVM server on Ubuntu 16.04 LTS (Xenial Xerus).

Preparation

sudo apt-get update && sudo apt-get upgrade

note
This tutorial is written for non-root users. Commands that require elevated privileges are prefixed with sudo.

Install PostgreSQL

Install PostgreSQL from the Ubuntu package repository:

sudo apt-get install postgresql postgresql-contrib

Configure PostgreSQL

Modify Postgres user

By default, PostgreSQL will create a Linux user named postgres to access the database software.

caveat
Users using postgres should not be used for other purposes (for example, to connect to other networks). Doing so will pose a serious threat to the security of the database.

sudo passwd postgres
su - postgres
psql -d template1 -c "ALTER USER postgres WITH PASSWORD 'newpassword';"

Please note that this user is different from the postgresLinux user. Linux users are used to access the database, and PostgreSQL users are used to perform management tasks on the database.

The password set in this step will be used to connect to the database via the network. By default, peer authentication will be used for local connections.

Create database

Run the commands in this section as the postgresLinux user.

createdb mytestdb
psql mytestdb
psql(9.5.2)
Type "help"for help.
​
mytestdb=#

This is the PostgreSQL client shell where you can issue SQL commands. To view the list of available commands, use the \h command. You can learn more about adding specific commands after \h.

Create table

This section contains examples of creating a test database using the first and last names of employees, assigning a unique key to each name. When creating your own table, you can specify as many parameters (columns) as needed and name them appropriately. Run the commands in this section from the PostgreSQL shell opened in step 2 of the Create Database section.

CREATE TABLE employees(employee_id int, first_name varchar, last_name varchar);
INSERT INTO employees VALUES(1,'John','Doe');
SELECT * FROM employees;

This will produce the following output:

 employee_id | first_name | last_name
- - - - - - - - - - - - - +- - - - - - - - - - - - +- - - - - - - - - - - 1| John       |Doe(1 row)

Enter the \q command to exit the PostgreSQL shell.

Create PostgreSQL role

PostgreSQL grants database access permissions through roles used to specify permissions. Roles can be understood as having functions similar to Linux "users". In addition, roles can also be created as a set of other roles, similar to Linux "groups." PostgreSQL roles are globally applicable, so you don’t need to create them. If you want to grant access to multiple databases on the same server, use the same twice Roles.

The example commands in this section should be run as the postgresLinux user.

createuser examplerole --pwprompt

If you need to delete a role, you can use the dropuser command instead of createuser.

psql mytestdb

By default, you will connect as the postgres database user.

GRANT ALL ON employees TO examplerole;

Type \q to exit the PostgreSQL shell.

Ensure the security of local PostgreSQL access

PostgreSQL uses peer authentication by default. This means that the database connection will be granted to local system users who own or have permissions to the connected database. This type of authentication is very useful in situations where a specific system user will run local programs (for example, scripts, CGI/FastCGI processes owned by different users, etc.), but for higher security, you may want to require a password to access Your database.

Unless otherwise noted, the commands in this section should be run as the postgresLinux user.

# " local" is for Unix domain socket connections only
local    all        all             peer`

Replace peer with md5 on this line to activate password authentication with MD5 hash.

exit
sudo service postgresql restart
su - postgres
psql -U examplerole -W mytestdb

You will be prompted to enter the password of the examplerole user and grant shell access to the database by psql. When using a database, you can use the \z command to check the access permissions of each table.

More information

For additional information on this topic, you may want to refer to the following resources. Although we hope to provide these useful, please note that we cannot guarantee the accuracy or timeliness of externally hosted materials.

PostgreSQL online documentation

psql man page

Self-built servers will inevitably encounter such problems. Configuring SSL is very troublesome. Although it is a kind of fun for some people, if you use it in a production environment, I still recommend that you directly use cloud relational database ), the cloud relational database allows you to easily deploy, manage and expand relational databases in the cloud, providing safe, reliable, scalable and flexible on-demand cloud database services. Tencent Cloud relational database provides MySQL, SQL Server, MariaDB, PostgreSQL database engine, and optimizes the performance of the database engine. The cloud relational database is a highly available hosting service that provides a complete set of database operation and maintenance solutions such as disaster recovery, backup, recovery, monitoring, migration, etc. It can free you from time-consuming database management tasks and let you have more Focus on your application and business more time.

If you don't want to configure MySQL, you can directly use the cloud database. TencentDB for MySQL allows users to easily deploy and use MySQL database in the cloud.

For more Linux and PostgreSQL tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.


Reference: "How to Install PostgreSQL on Ubuntu 16.04"

Recommended Posts

How to install PostgreSQL on Ubuntu 16.04
How to install PostgreSQL on Ubuntu 20.04
How to install and use PostgreSQL on Ubuntu 16.04
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to install Postgresql on CentOS 8
How to install MariaDB on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to install Nginx on Ubuntu 16.04
How to install OpenCV on Ubuntu 20.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install Go 1.6 on Ubuntu 16.04
How to install Go on Ubuntu 18.04
How to install MySQL on Ubuntu 14.04
How to install VLC on Ubuntu 18.04
How to install TeamViewer on Ubuntu 20.04
How to install Webmin on Ubuntu 20.04
Install postgresql-10 on Ubuntu 18.04
How to install Docker Compose on Ubuntu 18.04
How to install Ubuntu on Raspberry Pi
How to install Bacula Server on Ubuntu 14.04
How to install MySQL on Ubuntu 18.04 (linux)
How to install Ubuntu 19.10 on Raspberry Pi 4