02-Basic use of PostgreSQL

1. Database operation

①: Log in to the database

psql -U postgres -d postgres -h 127.0.0.1

image.png

②: View all databases

\l

image.png

③: Create database

# Create a database named mydb
create database mydb;

image.png

④:Switch database

# \c database name
\c mydb

image.png

⑤: Delete database

# Before deleting, make sure the database is not connected
drop database mydb;

image.png

2. Database table operations

①: Numeric type

image.png

image.png

image.png

②: Data table operation

1. Create table

1. The difference between sequence, serial and identity among the three auto-increment columns in PostgreSQL

image.png

2. Create table

create table test(id serial primary key, name varchar(255));

image.png

2. Insert data into the table

insert into test(name) values('Anne');

image.png

3. View all tables under the current database

\d

image.png

4. View the table structure, equivalent to desc

\d test(surface)

image.png

5. Query all data

image.png

3. Schema

①: Introduction

PostgreSQL schema (Schema) can be understood as a collection of tables (similar to Oracle’s table space concept).

A schema can contain views, indexes, data types, functions and operators, etc.

The same object name can be used in different schemas without conflict, for example schema1 and myschema can both contain a table named mytable.

Advantages of using patterns:

  1. Allow multiple users to use a database without interfering with each other
  2. Organize database objects into logical groups for easier management
  3. Objects of third-party applications can be placed in separate schemas so that they do not conflict with the names of other objects

Schemas are similar to operating system level directories, but schemas cannot be nested.

②: Create a new Schema

1. Before creation (there is a default public) and the test table created before is also included

image.png

2. Create a new Schema

create schema newSchema;

image.png

image.png

3. Create a table test in the new Schema

create table newSchema.test(id serial primary key, name varchar(255));

image.png

image.png

4. How to back up PostgreSQL database

If you are using PostgreSQL in a production environment, be sure to take precautions to ensure that your users’ data is not lost.

①:Must-see

1. Login

image.png

2. Note that operations must be performed in the shell console instead of the database console

image.png

②: Single database

PostgreSQL provides the pg_dump utility to simplify the process of backing up a single database. This command must be run as a user with read permissions on the database to be backed up.

1. Backup

image.png

There are several backup format options:

  • *.bak: compressed binary format

  • *.sq1: Clear text dump

  • *.tar: tarball

#Backup database
$ pg_dump -U postgres -f /tmp/postgres.sql postgres (export the postgres database and save it as postgres.sq1)
$ pg_dump -U postgres -f /tmp/postgres.sql-t test postgres (export the data of table test in the postgres database)
$ pg_dump -U postgres -F t -f /tmp/postgres.tar postgres (export the postgres database and compress it in tar format and save it as postgres.tar)

#Restore database
$ psql -U postgres -f /tmp/postgres.sql bk01 (restore postgres.sq1 data to bk01 database)
#pg_restore--Restore the PostgreSQL database from the backup file created by pg_dump. Used to restore the PostgreSQL database in any non-plain text format dumped by pg_dump.
$ pg_restore-Upostgres-dbk01/tmp/postgres.tar (restore postgres.tar data to bk01 database)

1. Dump the contents of the database to a file by running the following command. Replace dbname with the name of the database to be backed up.

 # Specify the file output directory through -f
    pg_dump -f /usr/local/src/mydb.sql mydb;

image.png

image.png

2. Recovery

1. Delete the backed up mydb database

image.png

2. Create a new empty database (mydb2)

image.png

3. Restore the database (-f specifies the backup file path)

 psql -U postgres -f /usr/local/src/mydb.sql mydb2;

image.png

You can see that the data recovery was successful

image.png

③:All databases

5. User operations

#View users
\du

#Create user and set password
CREATE USER 'username' WITH PASSWORD 'password' ;
CREATE USER test WITH PASSWORD 'test';

#Modify user password
$ ALTER USER ' username' WITH PASSWORD ' password' ;

#Database authorization, giving the specified account all permissions to the specified database
$ GRANT ALL PRIVILEGES ON DATABASE ' dbname' TO ' username' ;

#Grant database mydb permissions to test
GRANT ALL PRIVILEGES ON DATABASE mydb TO test;

#But at this time the user still does not have read and write permissions and needs to continue the authorization form
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO xxx;

#Note that the sq1 statement must be executed in the database to be operated
#Remove all permissions of the specified database for the specified account
REVOKE ALL PRIVILEGES ON DATABASE mydb from test

#delete users
drop user test

①: Create a user and set a password

1. Note: First switch to the database that needs to be operated

image.png

2. View all users

image.png

2. Create user

create user coke with password 'coke';

image.png

②: Modify user password

1. Change password

alter user coke with password 'new_coke';

image.png

2. Log in with new password

psql -U coke -d mydb2 -h 127.0.0.1;

image.png

3. Execute query sql

Newly created users do not have any permissions and need to be authorized

image.png

③: Database authorization

1. Database authorization, giving the specified account all permissions to the specified database

grant all privileges on database mydb2 to coke;

image.png

2. However, the user still does not have read and write permissions at this time and needs to continue the authorization form

grant all privileges on all tables in schema public to coke;

image.png

3. Test (already has permission)

image.png

④: Remove user data table permissions

revoke all privileges on database mydb2 from coke;

image.png

④: Remove user database permissions

revoke all privileges on database mydb2 from coke;

image.png

⑤: Delete user

drop user coke;

image.png