Ubuntu PostgreSQL installation and configuration

1. Installation#

1、 installation##

Use the following command to automatically install the latest version, here is 9.5

sudo apt-get install postgresql

After the installation is complete, the default will:

(1) Create a Linux user named "postgres"

(2) Create a default database account named "postgres" without a password as the database administrator

(3) Create a table named "postgres"

Some default information after installation is as follows:

config /etc/postgresql/9.5/main
data /var/lib/postgresql/9.5/main
locale en_US.UTF-8
socket /var/run/postgresql
port 5432

2、 psql command##

After installation, there will be a PostgreSQL client psql. Enter through sudo -u postgres psql, the prompt becomes: postgres=#

The basic commands for executing SQL statements and psql are available here. The basic commands available are as follows:

\ password: set password
\ q: exit
\ h: View the explanation of SQL commands, such as\h select。
\? : View the list of psql commands.
\ l: List all databases.
\ c [database_name]: Connect to other databases.
\ d: List all tables of the current database.
\ d [table_name]: List the structure of a table.
\ du: List all users.
\ e: Open a text editor.
\ conninfo: List the current database and connection information.

2. Modify the password of the database default account#

1、 log in##

The command to log in to the database using the psql command is:

psql -U dbuser -d exampledb -h 127.0.0.1-p 5432

The meaning of the parameters of the above command is as follows: -U specifies the user, -d specifies the database, -h specifies the server, and -p specifies the port.

After entering the above command, the system will prompt for the password of the dbuser user.

The psql command has a shorthand form:

If the current Linux system user is also a PostgreSQL user, you can omit the user name (the part of the -U parameter)

If there is a database with the same name as the current system user in PostgreSQL, the database name can also be omitted.

2、 Modify the password of the default administrator account##

Execute the psql client as the Linux user "postgres" (only this user has the psql command at this time) and enter the prompt interface of the client (here the system user name, database user name, and database name are all postgres, so abbreviations can be used form)

sudo -u postgres psql

postgres=# alter user postgres with password '123456';

In this way, the password of the administrator "postgres" is "123456".

Exit the psql client command: \q

To delete the administrator’s password, you can use the command: sudo -u postgres psql -d postgres

Three, modify the password of the Linux user#

This actually has little to do with installing postgresql.

Take the Linux user "postgres" as an example and run the passwd command on it:

zsm@ubuntu:/etc/postgresql/9.5/main$ sudo -u postgres passwd //You can also sudo passwd postgres
Changing password for postgres.(current) UNIX password: 
Enter newUNIX password: 
Retype newUNIX password: 
passwd: password updated successfully

Fourth, configure the database to allow remote connection access#

After the installation is complete, the database can only be connected locally by default, and other machines cannot access it. Configuration is required. (The following example opens the maximum connection permission, the actual configuration depends on your needs)

1、 Modify listening address##

sudo gedit /etc/postgresql/9.5/main/postgresql.conf

Remove the comment #listen_addresses ='localhost' and change it to listen_addresses ='*'

2、 Modify the IP segment of accessible users##

sudo gedit /etc/postgresql/9.5/main/pg_hba.conf

Add at the end of the file: host all all 0.0.0.0 0.0.0.0 md5, which means that any IP connection is allowed

3、 Restart the database##

sudo /etc/init.d/postgresql restart

Others: manage users, establish databases, etc.

Five, add new users and new databases#

Method 1: Use PostgreSQL client psql

Run the psql command of the system user "postgres" to enter the client:

sudo -u postgres psql

Create user "xiaozhang" and set password:

postgres=# create user xiaozhang with password '123456';

Create database exampledb, the owner is xiaozhang:

postgres=# create database exampledb owner xiaozhang;

Assign all permissions of the exampledb database to xiaozhang, otherwise xiaozhang can only log in to psql without any database operation permissions:

grant all privileges on database exampledb to xiaozhang;

Method 2: Use the shell command line##

After installing PostgreSQL, the createuser and createdb command line programs are provided.

First create the database user "xiaozhang1" and assign it as a super user:

sudo -u postgres createuser --superuser xiaozhang1;

Then log in to the psql console to set its password and exit:

zsm@ubuntu:~$ sudo -u postgres psql
psql(9.5.3)
Type "help"for help.
postgres=# \password xiaozhang1;
Enter newpassword: 
Enter it again: 
postgres=# \q

Then create the database and specify the owner under the shell command line:

sudo -u postgres createdb -O xiaozhang1 exampledb1;

Method 3: Use paadmin3 to connect to the database as an administrator and create

After the first and second operations, execute postgres=# \du to get the user list as follows:

Execute postgres=# \l to get the database list as follows:

To delete a user (such as deleting xiaozhang), first postgres=# drop database example; and then postgres=# drop user xiaozhang;.

Six, basic database operation commands#

# Create new table
CREATE TABLE user_tbl(name VARCHAR(20), signup_date DATE);
# Insert data
INSERT INTO user_tbl(name, signup_date)VALUES('Zhang San','2013-12-22');
# Select record
SELECT * FROM user_tbl;
# update data
UPDATE user_tbl set name ='Li Si' WHERE name ='Zhang San';
# Delete Record
DELETE FROM user_tbl WHERE name ='Li Si';
# Add field
ALTER TABLE user_tbl ADD email VARCHAR(40);
# Update structure
ALTER TABLE user_tbl ALTER COLUMN signup_date SET NOT NULL;
# Rename field
ALTER TABLE user_tbl RENAME COLUMN signup_date TO signup;
# Delete field
ALTER TABLE user_tbl DROP COLUMN email;
# Form rename
ALTER TABLE user_tbl RENAME TO backup_tbl;
# Delete table
DROP TABLE IF EXISTS backup_tbl;

Seven, reference materials#

Recommended Posts

Ubuntu PostgreSQL installation and configuration
Ubuntu16.04 installation and simple configuration
Configuration and beautification after Ubuntu installation (1)
Nginx installation and configuration load (ubuntu12.04)
Ubuntu configuration source and installation software
Ubuntu introduction and installation
DLNA/UPnP Server installation and configuration under Ubuntu 12.04
Debian and Ubuntu installation source configuration file description
Virtual machine installation and configuration ubuntu shared folder_
Kaldi installation and configuration graphic tutorials under Ubuntu
Installation, configuration and uninstallation of GitLab in Ubuntu19.1
Centos mysql installation and configuration
Centos7 installation and configuration prometheus
CentOS installation and configuration cmake
CentOS7 postgresql installation and use
Ubuntu16.04 mirror complete installation and configuration tutorial under VMware
Ubuntu12 step-by-step installation and configuration (system, FTP, TELNET.. settings)
Common exceptions and solutions for Ubuntu system installation and configuration
Centos7 installation and configuration of Jenkins
Centos7 hadoop cluster installation and configuration
Ubuntu16.04 configuration OpenCV3.4.2 and basic use
Redis installation under ubuntu and windows
Java-JDK installation and configuration under CentOS
CentOS 7 Tomcat service installation and configuration
CentOS NTP server installation and configuration
Ubuntu18.04 installation Anaconda3 and VSCode guide
Centos7 mysql database installation and configuration
ubuntu Docker installation and deployment of Rancher
ubuntu 18.04 early configuration
UBUNTU 16.04 installation diary
Tomcat installation and configuration under CentOS 7 (Tomcat startup)
MySQL 8.0 installation, deployment and configuration under CentOS 6/7
Ubuntu18.04 system installation and prerequisite software installation guide
ubuntu 1804 installation details
Installation and configuration of redis under centos7
Installation and uninstallation of CUDA under Ubuntu 16.04
Ubuntu18.04 Server version installation and use (graphic)
Installation and deployment of Nginx in Ubuntu
Installation and use of Win10 subsystem Ubuntu
Ubuntu nfs configuration
Ubuntu install PHP and PHP Nginx configuration method
Centos7 hive stand-alone mode installation and configuration
Ubuntu 14.04 16.04 Linux nvidia driver download and installation
Ubuntu20.04 configuration notes
Ubuntu installation record
Ubuntu 14.04 configuration record
ubuntu 18.04 installation (UEFI+GBT)
ubuntu installation error
Installation and configuration of JDK in CentOS 7 system
CentOS 6.5 system installation and configuration graphic tutorial (detailed graphic)
Installation of pulseaudio and API usage under Ubuntu 12.04
The basic configuration and interface beautification of Ubuntu
CentOS 7 installation and configuration graphic tutorials under VMware10
Installation and configuration of rsync server under CentOS 6.5
Installation and configuration of CentOS 7 in VMware Workstation
Technical corner | Windows 10/7 and Ubuntu 18.04 dual system installation
MySQL 8.0 installation, deployment and configuration tutorial on CentOS 8
Installation and use of SSH in Ubuntu environment
Manual for Ubuntu Installation
PyCUDA-Ubuntu 14.04 installation and testing
Summary: Ubuntu Python2.x and