"Understanding PostgreSQL" by Tu Yaofeng-Problems and solutions encountered in the process of learning PostgreSQL (under CentOS environment)

1. Planning the database path, that is, an error is reported when customizing the database path [initdb: The directory "/home/postgres/pgdata" already exists, but it is not empty. If you want to create a new database system, please delete or empty the directory "/ home/postgres/pgdata" or run initdb with parameters instead of "/home/postgres/pgdata".], [Initializing database… /sbin/restorecon set context /home/postgres/pgdata->unconfined_u:object_r:user_home_t:s0 failed:'Operation not permitted'/home/postgres/pgdata is not writeable by postgres] and [creating directory /home/postgres/pgdata… initdb: could not create directory "/home/postgres": Permission denied] The
solutions are as follows:
For errors [initdb: The directory "/home/postgres/pgdata" already exists, but is not empty. If you want to create a new database system, please delete or empty the directory "/home/postgres/pgdata" or run initdb with parameters Rather than "/home/postgres/pgdata".], the reason for this error is that the specified database path directory [/home/postgres/pgdata] is not an empty directory, so you need to clear all the contents, so that Can solve the problem.
For errors [Initializing database… /sbin/restorecon set context /home/postgres/pgdata->unconfined_u:object_r:user_home_t:s0 failed:'Operation not permitted'/home/postgres/pgdata is not writeable by postgres] and [creating directory /home/postgres/pgdata… initdb: could not create directory "/home/postgres": Permission denied], the reason for these two errors is that a user [postgres] will be automatically created after PostgreSQL is installed and started. Use the command [ su-postgres] After switching to this user, execute the command [initdb -D /home/postgres/pgdata]. Due to the permissions of the user [postgres] on the directory [/home/postgers/pgdata], these two errors may occur. The solution is to change the users and user groups of the directories [postgres] and [pgdata] to users [postgres], and use the command [chown].


2. Switch to the user [postgres] and run the command [pg_ctl start data path] to start the database, an error [port 5432 is occupied] is reported.
Solution: Restart, or use the command [pg_ctl status -D data path] to see if the database is running. If it is running, you can first execute the command [pg_ctl stop -D data path] to close it, and then restart it.


3. An error is reported when performing update and delete operations on the database [IntegrityError: insert or update on table "..." violates foreign key constraint "..."].
Example of the problem: there is a primary key in the news table, and a primary key and a foreign key in the comment_news table. A constraint is established between the foreign key of comment_news and the primary key of news in the database. The content of the foreign key in comment_news has content other than the news primary key. So just report an error, just change it to the same one.
Solution: If you delete the data in the data table, you need to delete the data in the table with the foreign key first, and then delete the corresponding data in the primary key. For example, there is a primary key in the news table, and a primary key and a foreign key in the comment_news table. First delete the corresponding data in the comment_news table, and then delete the content in the news table.


4. An error is reported when inserting elements into the database using the insert statement [duplicate key value violates unique constraint]

  • Solution: One possible situation for this kind of error is that the inserted data already exists in the database before, and this situation will occur if you insert it again. Therefore, we can think about the solution in terms of whether there will be duplicate data.

5. About the use of sequence to realize the self-increment operation of a certain integer type field in the data table.

  • First create a sequence. The code is shown below.

    create sequence seq_test
    start with 1
    increment by 1
    no minvalue
    no maxvalue
    cache 1;
    

    Where seq_test is the name of the sequence, start with 1 means starting from 1, and increment by 1 means increasing by 1 each time.
    Afterwards, you can use the command [\d seq_test] to view the related attributes of the sequence. As shown below.
    Insert picture description here

  • Then create a table. code show as below.

    create table tb_test04(           
    a int not null default nextval('seq_test'), 
    b text not null, 
    c timestamp
    );
    

    In the table created above, the field a needs to realize the self-increment of the value, directly call the nextval function to traverse the sequence [seq_test] just created.


6. When creating a table, specify the type of a field as [serial], so that when inserting data, the value of the field will increase automatically.

  • First create a data table. The code is shown below.
    create table tb_test05(
    a serial not null, 
    b text not null, 
    c timestamp
    );
    
    The prompt after successful creation is as follows.
    Insert picture description here
    As can be seen from the above prompt, an implicit sequence is automatically created. You can observe the structure of the created sequence, as shown in the figure below.
    Insert picture description here
    As you can see from the above figure, the default creation sequence is exactly the same as the sequence created by the fifth point summarized above. You can continue to observe the structure of the table you just created, as shown in the following figure.
    Insert picture description here
    It can be seen from the figure above that the default value of field a is the value in the sequence.
  • When inserting a new piece of data later, if the value of field a is not specified, the default value will be used.

7. Why is there duplicate data in multi-table query?

  • It is possible that there are duplicate records in a column in a multi-table query, so the duplicate records will be matched multiple times when connecting.
    As shown in the figure below.
    Insert picture description here
    The rightmost column shows that both records are three.
    In fact, for 149671 it was matched three times before. As shown below.
    Insert picture description here

Guess you like

Origin blog.csdn.net/ISs_Cream/article/details/111222061