• How to install PostgreSQL in Centos8


    List out the available streams for the postgresql module using the dnf command:

    1. [root@MaxwellDBA home]# dnf module list postgresql
    2. Repository extras is listed more than once in the configuration
    3. Invalid configuration value: failovermethod=priority in /etc/yum.repos.d/CentOS-epel.repo; Configuration: OptionBinding with id "failovermethod" does not exist
    4. Last metadata expiration check: 0:37:44 ago on Tue 30 Aug 2022 12:23:12 PM CST.
    5. CentOS-8.5.2111 - AppStream - mirrors.aliyun.com
    6. Name Stream Profiles Summary
    7. postgresql 9.6 client, server [d] PostgreSQL server and client module
    8. postgresql 10 [d] client, server [d] PostgreSQL server and client module
    9. postgresql 12 client, server [d] PostgreSQL server and client module
    10. postgresql 13 client, server [d] PostgreSQL server and client module
    11. CentOS Linux 8 - AppStream
    12. Name Stream Profiles Summary
    13. postgresql 9.6 client, server [d] PostgreSQL server and client module
    14. postgresql 10 [d] client, server [d] PostgreSQL server and client module
    15. postgresql 12 client, server [d] PostgreSQL server and client module
    16. postgresql 13 client, server [d] PostgreSQL server and client module
    17. Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

    To enable the module stream for Postgres version 12, run the following command:

     

    1. [root@MaxwellDBA home]# sudo dnf module enable postgresql:12
    2. Repository extras is listed more than once in the configuration
    3. Invalid configuration value: failovermethod=priority in /etc/yum.repos.d/CentOS-epel.repo; Configuration: OptionBinding with id "failovermethod" does not exist
    4. Last metadata expiration check: 0:41:11 ago on Tue 30 Aug 2022 12:23:12 PM CST.
    5. Dependencies resolved.
    6. ==========================================================================================================================================================================
    7. Package Architecture Version Repository Size
    8. ==========================================================================================================================================================================
    9. Enabling module streams:
    10. postgresql 12
    11. Transaction Summary
    12. ==========================================================================================================================================================================
    13. Is this ok [y/N]: y
    14. Complete!
    15. [root@MaxwellDBA home]#

    After enabling the version 12 module stream, you can install the postgresql-server package to install PostgreSQL 12 and all of its dependencies:

    1. [root@MaxwellDBA home]# sudo dnf install postgresql-server
    2. Repository extras is listed more than once in the configuration
    3. Invalid configuration value: failovermethod=priority in /etc/yum.repos.d/CentOS-epel.repo; Configuration: OptionBinding with id "failovermethod" does not exist
    4. Last metadata expiration check: 0:43:08 ago on Tue 30 Aug 2022 12:23:12 PM CST.
    5. Dependencies resolved.
    6. ==========================================================================================================================================================================
    7. Package Architecture Version Repository Size
    8. ==========================================================================================================================================================================
    9. Installing:
    10. postgresql-server x86_64 12.9-1.module_el8.5.0+1061+5aa9de58 AppStream 5.6 M
    11. Installing dependencies:
    12. libpq x86_64 13.3-1.el8_4 AppStream 197 k
    13. postgresql x86_64 12.9-1.module_el8.5.0+1061+5aa9de58 AppStream 1.5 M
    14. Transaction Summary
    15. ==========================================================================================================================================================================
    16. Install 3 Packages
    17. Total download size: 7.3 M
    18. Installed size: 28 M
    19. Is this ok [y/N]: y
    20. Downloading Packages:
    21. (1/3): libpq-13.3-1.el8_4.x86_64.rpm 1.1 MB/s | 197 kB 00:00
    22. (2/3): postgresql-12.9-1.module_el8.5.0+1061+5aa9de58.x86_64.rpm 4.3 MB/s | 1.5 MB 00:00
    23. (3/3): postgresql-server-12.9-1.module_el8.5.0+1061+5aa9de58.x86_64.rpm 6.9 MB/s | 5.6 MB 00:00
    24. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    25. Total 9.0 MB/s | 7.3 MB 00:00
    26. Running transaction check
    27. Transaction check succeeded.
    28. Running transaction test
    29. Transaction test succeeded.
    30. Running transaction
    31. Preparing : 1/1
    32. Installing : libpq-13.3-1.el8_4.x86_64 1/3
    33. Installing : postgresql-12.9-1.module_el8.5.0+1061+5aa9de58.x86_64 2/3
    34. Running scriptlet: postgresql-server-12.9-1.module_el8.5.0+1061+5aa9de58.x86_64 3/3
    35. Installing : postgresql-server-12.9-1.module_el8.5.0+1061+5aa9de58.x86_64 3/3
    36. Running scriptlet: postgresql-server-12.9-1.module_el8.5.0+1061+5aa9de58.x86_64 3/3
    37. [/usr/lib/tmpfiles.d/postgresql.conf:1] Line references path below legacy directory /var/run/, updating /var/run/postgresql → /run/postgresql; please update the tmpfiles.d/ drop-in file accordingly.
    38. Verifying : libpq-13.3-1.el8_4.x86_64 1/3
    39. Verifying : postgresql-12.9-1.module_el8.5.0+1061+5aa9de58.x86_64 2/3
    40. Verifying : postgresql-server-12.9-1.module_el8.5.0+1061+5aa9de58.x86_64 3/3
    41. Installed:
    42. libpq-13.3-1.el8_4.x86_64 postgresql-12.9-1.module_el8.5.0+1061+5aa9de58.x86_64 postgresql-server-12.9-1.module_el8.5.0+1061+5aa9de58.x86_64
    43. Complete!
    44. [root@MaxwellDBA home]#

    Step 2 — Creating a New PostgreSQL Database Cluster

    You have to create a new PostgreSQL database cluster before you can start creating tables and loading them with data. A database cluster is a collection of databases that are managed by a single server instance. Creating a database cluster consists of creating the directories in which the database data will be placed, generating the shared catalog tables, and creating the template1 and postgres databases.

    The template1 database is a template of sorts used to create new databases; everything that is stored in template1, even objects you add yourself, will be placed in new databases when they’re created. The postgres database is a default database designed for use by users, utilities, and third-party applications.

    The Postgres package we installed in the previous step comes with a handy script called postgresql-setup which helps with low-level database cluster administration. To create a database cluster, run the script using sudo and with the --initdb option:

    1. [root@MaxwellDBA home]# sudo postgresql-setup --initdb
    2. * Initializing database in '/var/lib/pgsql/data'
    3. * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
    4. [root@MaxwellDBA home]#

    Now start the PostgreSQL service using systemctl:

    1. [root@MaxwellDBA home]# sudo systemctl start postgresql
    2. [root@MaxwellDBA home]#

    Then, use systemctl once more to enable the service to start up whenever the server boots:

    1. [root@MaxwellDBA home]# sudo systemctl enable postgresql
    2. Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/systemd/system/postgresql.service.
    3. [root@MaxwellDBA home]#

    Now that PostgreSQL is up and running, we will go over using roles to learn how Postgres works and how it is different from similar database management systems you may have used in the past.

    Step 3 — Using PostgreSQL Roles and Databases

    PostgreSQL uses a concept called roles to handle client authentication and authorization. These are in some ways similar to regular Unix-style accounts, but Postgres does not distinguish between users and groups and instead prefers the more flexible term role.

    Upon installation, Postgres is set up to use ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account. If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.

    The installation procedure created a user account called postgres that is associated with the default postgres role. In order to use PostgreSQL, you can log in to that account.

    There are a few ways to use this account to access the PostgreSQL prompt.

    Switching Over to the postgres Account

    Switch over to the postgres account on your server by typing:

    [root@MaxwellDBA home]# sudo -i -u postgres

    You can now access a Postgres prompt immediately by typing:
     

    1. [postgres@MaxwellDBA ~]$ psql
    2. psql (12.9)
    3. Type "help" for help.

    This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.

    Exit out of the PostgreSQL prompt by typing:

    postgres=# \q

    This will bring you back to the postgres account’s Linux command prompt. Now return to your original account with the following:

    1. [postgres@MaxwellDBA ~]$ exit
    2. logout
    3. [root@MaxwellDBA home]#

    Accessing a Postgres Prompt Without Switching Accounts

    You can also run commands with the postgres account directly using sudo.

    For instance, in the previous example, you were instructed to access the Postgres prompt by first switching to the postgres user and then running psql to open the Postgres prompt. As an alternative, you could do this in one step by running the single command psql as the postgres user with sudo, like this:

    1. [root@MaxwellDBA home]# sudo -u postgres psql
    2. psql (12.9)
    3. Type "help" for help.
    4. postgres=# select version();
    5. version
    6. ------------------------------------------------------------------------------------------------------------
    7. PostgreSQL 12.9 on x86_64-redhat-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4), 64-bit
    8. (1 row)
    9. postgres=# \q

    In this step, you used the postgres account to reach the psql prompt. But many use cases require more than one Postgres role. Read on to learn how to configure new roles.

    1. axwellDBA home]# sudo -i -u postgres
    2. [postgres@MaxwellDBA ~]$ createuser --interactive
    3. Enter name of role to add: maxwell
    4. Shall the new role be a superuser? (y/n) y
    5. [postgres@MaxwellDBA ~]$

    If, instead, you prefer to use sudo for each command without switching from your normal account, type:

    sudo -u postgres createuser --interactive

    You can get more control by passing some additional flags. Check out the options by looking at the man page for createuser:

    man createuser

    Step 5 — Creating a New Database

    Another assumption that the Postgres authentication system makes by default is that for any role used to log in, that role will have a database with the same name which it can access.

    This means that if the user you created in the last section is called maxwell, that role will attempt to connect to a database which is also called maxwellby default. You can create such a database with the createdb command.

    If you are logged in as the postgres account, you would type something like:

    1. [postgres@MaxwellDBA ~]$ createdb maxwelldb
    2. [postgres@MaxwellDBA ~]$

    If, instead, you prefer to use sudo for each command without switching from your normal account, you would type:

    sudo -u postgres createdb maxwelldb

    This flexibility provides multiple paths for creating databases as needed.

    Now that you’ve created a new database, you will log in to it with your new role.

    1. [root@MaxwellDBA home]# sudo -i -u postgres
    2. [postgres@MaxwellDBA ~]$ createdb maxwell
    3. [postgres@MaxwellDBA ~]$ exit
    4. logout
    5. [root@MaxwellDBA home]# sudo -i -u maxwell
    6. [maxwell@MaxwellDBA ~]$ psql
    7. psql (12.9)
    8. Type "help" for help.
    9. maxwell=#

    Once this new account is available, you can either switch over and then connect to the database by first typing:

    1. [root@MaxwellDBA home]# sudo -i -u maxwell
    2. [maxwell@MaxwellDBA ~]$ psql

    If you want your user to connect to a different database, you can do so by including the -d flag and specifying the database, like this:

    1. [maxwell@MaxwellDBA ~]$ psql -d maxwelldb
    2. psql (12.9)
    3. Type "help" for help.
    4. maxwelldb=#

    Once logged in, you can check your current connection information by typing: 

    1. [maxwell@MaxwellDBA ~]$ psql -d maxwelldb
    2. psql (12.9)
    3. Type "help" for help.
    4. maxwelldb=# \conninfo
    5. You are connected to database "maxwelldb" as user "maxwell" via socket in "/var/run/postgresql" at port "5432".
    6. maxwelldb=#

    This is useful if you are connecting to non-default databases or with non-default users.

    Having connected to your database, you can now try out creating and deleting tables.

    Step 7 — Creating and Deleting Tables

    Now that you know how to connect to the PostgreSQL database system, you can learn some basic Postgres management tasks.

    First, create a table to store some data. As an example, you will make a table that describes some playground equipment.

    The basic syntax for this command is as follows:

    1. CREATE TABLE table_name (
    2. column_name1 col_type (field_length) column_constraints,
    3. column_name2 col_type (field_length),
    4. column_name3 col_type (field_length)
    5. );
    1. maxwelldb=# CREATE TABLE playground (
    2. maxwelldb(# equip_id serial PRIMARY KEY,
    3. maxwelldb(# type varchar (50) NOT NULL,
    4. maxwelldb(# color varchar (25) NOT NULL,
    5. maxwelldb(# location varchar(25) check (location in ('north', 'south', 'west', 'east', 'northeast', 'southeast', 'southwest', 'northwest')),
    6. maxwelldb(# install_date date
    7. maxwelldb(# );
    8. CREATE TABLE
    9. maxwelldb=# \d
    10. List of relations
    11. Schema | Name | Type | Owner
    12. --------+-------------------------+----------+---------
    13. public | playground | table | maxwell
    14. public | playground_equip_id_seq | sequence | maxwell
    15. (2 rows)
    16. maxwelldb=# \dt
    17. List of relations
    18. Schema | Name | Type | Owner
    19. --------+------------+-------+---------
    20. public | playground | table | maxwell
    21. (1 row)
    22. maxwelldb=#

    Step 8 — Adding, Querying, and Deleting Data in a Table

    Step 9 — Adding and Deleting Columns from a Table

    Step 10 — Updating Data in a Table

    1. maxwelldb=# INSERT INTO playground (type, color, location, install_date) VALUES ('slide', 'blue', 'south', '2017-04-28');
    2. INSERT 0 1
    3. maxwelldb=# INSERT INTO playground (type, color, location, install_date) VALUES ('swing', 'yellow', 'northwest', '2018-08-16');
    4. INSERT 0 1
    5. maxwelldb=# select * from playground;
    6. equip_id | type | color | location | install_date
    7. ----------+-------+--------+-----------+--------------
    8. 1 | slide | blue | south | 2017-04-28
    9. 2 | swing | yellow | northwest | 2018-08-16
    10. (2 rows)
    11. maxwelldb=# DELETE FROM playground WHERE type = 'slide';
    12. DELETE 1
    13. maxwelldb=# select * from playground;
    14. equip_id | type | color | location | install_date
    15. ----------+-------+--------+-----------+--------------
    16. 2 | swing | yellow | northwest | 2018-08-16
    17. (1 row)
    18. maxwelldb=# select * from playground;
    19. equip_id | type | color | location | install_date
    20. ----------+-------+--------+-----------+--------------
    21. 2 | swing | yellow | northwest | 2018-08-16
    22. (1 row)
    23. maxwelldb=# ALTER TABLE playground ADD last_maint date;
    24. ALTER TABLE
    25. maxwelldb=# select * from playground;
    26. equip_id | type | color | location | install_date | last_maint
    27. ----------+-------+--------+-----------+--------------+------------
    28. 2 | swing | yellow | northwest | 2018-08-16 |
    29. (1 row)
    30. maxwelldb=# ALTER TABLE playground DROP last_maint;
    31. ALTER TABLE
    32. ^
    33. maxwelldb=# select * from playground;
    34. equip_id | type | color | location | install_date
    35. ----------+-------+--------+-----------+--------------
    36. 2 | swing | yellow | northwest | 2018-08-16
    37. (1 row)
    38. maxwelldb=# UPDATE playground SET color = 'red' WHERE type = 'swing';
    39. UPDATE 1
    40. maxwelldb=# select * from playground;
    41. equip_id | type | color | location | install_date
    42. ----------+-------+-------+-----------+--------------
    43. 2 | swing | red | northwest | 2018-08-16
    44. (1 row)
    45. maxwelldb=#
  • 相关阅读:
    优秀的 Modbus 主站(主机、客户端)仿真器、串口调试工具
    3.代码生成器编写
    4 Paimon数据湖之Hive Catalog的使用
    使用自开发的代理服务器解决 SAP UI5 FileUploader 上传文件时遇到的跨域访问错误试读版
    ClickHouse 如何实现数据一致性
    LQ0017 排列字母【排序】
    数字人技术在直播场景下的应用
    移动通信覆盖自愈的研究与实现
    Java当中的队列
    关于js实现斐波那契数列的一些思考(递归、循环、尾递归优化)
  • 原文地址:https://blog.csdn.net/u011868279/article/details/126601734