Environment construction for OSS-DB Silver # 2_Preparation 1-Create a user account to operate PostgreSQL on CentOS7

Introduction

For learning OSS-DB Silver, we built an environment on a Windows PC.

I can't find an article written by a programming beginner, probably because OSS-DB Silver is a qualification for those who are already working as programmers. I wrote this article for "** beginners of programming and learning to acquire her OSS-DB Silver **" like me. Rather than simply summarizing how to build an environment, I've written a lot of my thoughts about the troublesome and confused places.

Last time, I built a Linux environment (virtual environment) on a Windows PC. "Building an environment for OOSS-DB Silver # 1_Create a Linux environment (CentOS7 virtual environment) with VirtualBox/Vagrant"

This time, we will move on to the next task. "Create a user account to operate PostgreSQL in the built Linux environment"

Textbook applicable part = "OSS textbook OSS-DB Silver Ver2.0 compatible (green book)") Chapter 3-Installation "Preparation (p48)"

** [Point of stumbling] -How to use CentOS7 (Linux). Where and how should I write the command? What are the command options? -How to use CentOS7 (Linux). How should I use the root user and the general user properly? ** **

Work: Create a user account for PostgreSQL operations

The textbook (p48) says:

// 3-1-1 Preparation // First, create a user account to operate PostgreSQL. In this document, we will create a commonly used postgres user. (Refer to the command below) In this manual, the command line starting with a pound (#) is executed by the root user. Command lines starting with a dollar mark ($) are executed by a general user.

【command】

$ su - 
# useradd -m -d /home/postgres postgres
# passwd postgres
# su - postgres

We will create a user to operate PostgreSQL.

The "user" created here is a user managed by the OS (CentOS7). It is not a user managed by middleware (PostgreSQL). Be careful not to mix.

After reading this far, I have some questions, so I will investigate them one by one.

** What is a user? ** ** The user account that logs in when using Linux is called a Linux user. Linux is the same as a general OS, and you need to log in as a user to actually use it. Two things are required to log in: "user name and password". (Reference: "[Even beginners can understand!] What is a Linux user?")

** What is the root user? ** ** Administrator of Linux etc. Administrator privileges. The greatest user. The user who must protect the most for security. Only borrow root privileges when necessary (* below). It seems that it will also accept commands to delete itself. That's horrible. Use caution! !!

** What is a general user? ** ** In the case of Linux, it is customary for the administrator who installed it to create a separate general user and operate it with root privileges only when necessary. In the case of general users, their own directory is assigned and they work in it. If you want to temporarily borrow root privileges, use commands such as "su" and "sudo". In that case, even if it is temporary, the authority of the administrator is borrowed, so it must be operated carefully. (By the way, I was worried that the textbooks generally use "user" and others use "user". Are you using them properly? In the reference article, it was written as "general user, root user". .) (Reference: "[Even beginners can understand!] What is a Linux user?")

Also, the textbook said, "In this book, the command line starting with ** (#) is executed by the root user, and the command line starting with ($) is executed by the general user **". Should I write it? ** I tried googled with "postgreSQL user creation" etc., but only the method of creating a user in the environment where PostgreSQL is already installed is a hit. I couldn't find an article like a textbook that takes the method of creating a PostgreSQL user account before installing postgreSQL. → I solved it while reading the meaning of the command below. ** I was instructing to switch users with a command **.

Understand the meaning of the command

The textbook provided the four lines of command above, but did not explain what it was. I will read and understand each one.

Here is the final read. The meaning of the command is in "", and the memo is in parentheses. The command line starting with ($) is the general user, and the command line starting with (#) is the root user.

(Run as a general user)
$ su -
"Change the OS user to the root user"
(Switch from general user to root user here)
(The following is executed as the root user as it is)
# useradd -m -d /home/postgres postgres
"Register as a new user"
"If the user's home directory (the directory when you logged in) does not exist, create it."
"The home directory of the newly created user/home/postgres Specify postgres "
# passwd postgres
"Change the password for user postgres"
# su - postgres
"Change the OS user to the postgres user"

Below are the details.

①**$ su -** The su command is a command to "switch users". "-" Is an instruction that "the switching destination user name is root". In other words, it means "change the user to the root user". Why is it "su-" instead of "su root"? → Since the number of times to switch to the root user is large, it is omitted. (Reference: "su --Become a command root user")

②**# useradd -m -d /home/postgres postgres**

-** useradd ** = "Register new user" command. Here, it is used in the form (format) of "useradd [option] new user name".

-** Option ** = If the command argument (following string) begins with a hyphen "-" or "-", that argument is optional. In other words, "-m" and "-d" are options here. (Reference: "Notes on command line options for Linux systems") It seems that there are short and long options to write. See "-m" etc. below for specific examples.

-**-M ** = Optional. The meaning is "If the user's home directory (the directory when logging in) does not exist, create it" Short option. The long option is "--create-home" (Reference: [useradd] command-create a new user)

-**-D ** = Optional. The meaning is "specify the home directory of the newly created user" (Reference: "Linux command [useradd] Create new user")

-**/home/postgres postgres ** = Home directory you want to specify.

③ ** # passwd postgres ** = "Change user postgres password" (Reference: "User and password settings")

④ ** # su --postgres ** = "Switch to OS postgres user" (Reference: "User and password settings") → ** Question: This "-" means "root" Don't you? ** When I looked up "$ su-", it should have been ""-"means the root user".

Start work

Now that I have somehow understood each term, it's time to start working.

First, click Oracle VM VirtualBox from the start screen of your PC. Oracle VM VirtualBox Manager is launched. Click training. Click Start. Virtual CentOS 7 starts.

Here is the launched screen. 無題.png

I stopped here. Where on earth should I write the above command? ** **

Also, I tried to take a screenshot with "Windows key + PrtSc" to paste this black screen on Qiita, but I could not take a screenshot with this operation when this black screen of CentOS 7 was selected. If I selected another window in the host OS and had the black screen of CentOS 7 displayed as the background, I could take a screenshot with the same operation. It's natural because the OS is different.

In addition, I tried to copy a command with a black screen by selecting it with the mouse, but the mouse did not work on the black screen. Will this also read the mouse if I change the setting of CentOS 7 itself and specify the mouse? When I looked it up, I found a method using Guest Addtions. But for now, let's go through and move forward.

Where should I write the command?

First, copy "$ su-" with "Ctrl + C" and paste it on the displayed screen with "Ctrl + V". → "^" is displayed. "Ctrl + V" again. → "U" is displayed. 0.png

^ U ^ U ^ U ^ U ^ U ^ ... So that's it. "Copy the one on the host OS (Windows 10) and paste it on the guest OS (CentOS 7)" It seems that you cannot work across OSs. There seems to be some way to do this as well, but for now I'll go through it.

Next, I will try to hit it with the keyboard normally. Reboot CentOS7, type "su-" on the keyboard and press Enter. 3.png "Password:" is displayed.

next Restart CentOS7, type "$ su-" on the keyboard, and press Enter. 4.png

The message "Password:" was displayed again. The operation is the same with or without the "$". By the way, after a while, the screen looked like this. 5.png

?? Why did the screen change without permission? And what is “Localhost Login:”?

Upon examination, "Localhost Login:" is a command that prompts you to log in to CentOS7. At that time, it is necessary to enter the password set when installing CentOS 7. ・ ・ ・ No password has been set. During the installation, I was not prompted to set a password. Does that mean I need to set it now?

While doing so, all the commands on the black screen disappeared and turned into a blank black screen. Why ---! ?? ?? ?? Will the commands disappear without permission over time without doing anything? I already felt that it would be better to learn how to use CentOS 7 itself before setting up PostgreSQL ...

(On the black screen, when I hit the keyboard, the commands that were displayed earlier were displayed as before. After a while without doing anything, only the screen seems to turn black.)

After reading a few articles, This article: "Build a development environment on Windows quickly with Vagrant + VirtualBox" " ** User: vagrant Password: vagrant ** I tried typing and it worked. Some of the commands I entered when building CentOS 7 with vagrant Especially, I can't find the word "password". It remains a mystery at what stage this password was set. But now I can't solve this mystery, so I'll go through it.

Anyway, I was able to log in to CentOS 7 as a general user (user name: vagrant password: vagrant).

Since "[vagrant @ localhost ~] " is displayed on the bottom line Then I typed "su-". You don't have to enter the "" yourself, it will be displayed without permission. Enter。 "-Bash: su-: command not found (line feed) [vagrant @ localhost ~] $" is displayed. Try inserting a half-width space between su and-. 「su -」Enter。 "Password:" is displayed. It went well! Space is important, isn't it?

Well, I was asked again for a password that I didn't remember setting. As before, this article "Vagrant + VirtualBox quickly builds a development environment on Windows ”. User: root ** Password: vagrant ** I will use this one that was in the article.

I try typing "vagrant" to the right of "Password:", but nothing is displayed. The cursor doesn't move either. However, I remember seeing in the articles I've seen so far that nothing is displayed when entering the password, but it is okay because the OS recognizes it. Enter as it is. The Password remains hidden, but the OS responds with "[root @ localhost ~] #" on the last line. It's a success! The textbook said, "In this book, command lines that start with a pound are executed as the root user." Even on the OS, the root user seems to be displayed as "#".

This completes the first line (out of four lines) of the command.

Next is the second line (out of four lines). 「useradd -m -d /home/postgres postgres」、Enter。 「passwd postgres」、Enter。 Here, "Changing password for user postgres. (Line feed) New password:" is displayed.

It is a sentence that is not written in the textbook. The meaning of "passwd postgres" is "change the password of user postgres", so does that mean that it is displayed in English? I haven't set a password yet, but it feels strange to "Changing". However, I will try to set an arbitrary password for the time being.

"Postgres" (← not displayed on OS), Enter. → "BAD PASSWORD: The password contains the user name in some form (line feed) Retype new password:" is displayed. You shouldn't include postgres in your password, right?

Then 「vagrant、Enter。 → "Sorry, passwords do not match. (Line feed) New password:" is displayed. What does it mean to say "doesn't match" when you haven't set a password yet? .. ..

Stop and check. I googled with "passwd postgres, CentOS Postgres source" etc. However, I couldn't find an article that explains this part. The OSS-DB Official Commentary Page did not mention this preparation at all, nor did the Documents mention it.

It's a crap. 「pspsp」、Enter。 → "BAD PASSWORD: The password is a palindrome (line feed) Retype new password:" is displayed. It seems that palindrome = palindrome (words and sentences that can be read in the same way whether read from the front or after). The palindrome is useless.

What should I do! 「pos10」、Enter。 → "Sorry, passwords do not match. (Line feed) New password:" is displayed. → "Does not match" again. Is this a reconfirmation of the password you just typed?

「pos10」、Enter。 → "Retype new password:" is displayed. Maybe it's just reconfirmed! →「pos10」、Enter。 → "passwd: all authentication tokens updated successfully. (Line feed) [root @ localhost ~] #" is displayed.

Wow! It's a success! You were simply "reconfirming your password". It feels like where you're stumbling. But anyway, I was able to go through this stage.

Type the following command. 「su - postgres」、Enter。 → "[postgres @ localhost ~] $" is displayed. The OS user has been changed to the postgres user.

Finally, the 4 lines of commands that were in the textbook are complete.

Summary

I will summarize all the work from starting CentOS 7 to executing the four lines of the textbook command.

(Select the corresponding OS of Virtual Box and click "Start")
(「(Omission)eth1:"link becomes ready" is displayed)
vagrant
(And click Enter. "Password:Is displayed)
vagrant
(And type(Not displayed on the screen), Click Enter. "Last login:(abridgement)[vagrant@localhost ~]$Is displayed. General user status.)
su -
(And click Enter. "Password:Is displayed)
(Meaning = "Change the OS user to the root user". Switch from general user to root user here)
vagrant
(And type(Not displayed on the screen), Click Enter. "Last login:(abridgement)[root@localhost ~]#Is displayed) 
useradd -m -d /home/postgres postgres
(And click Enter)
(Meaning = "Register as a new user" "If the user's home directory (directory when logged in) does not exist, create it" "Create a new user's home directory"/home/postgres Specify postgres ")
passwd postgres
(And click Enter. "Changing password for user postgres.New password:Is displayed)
(Meaning = "Change password for user postgres")
Any password
(Type in and click Enter. "Retype new password:Is displayed)
Any password you set
(Type in and click Enter. "Passwd: all authentication tokens updated successfully.(new line)[root@localhost ~]#Is displayed)
su - postgres
(And click Enter. "[postgres@localhost ~]$Is displayed)
(Meaning = "Change OS user to postgres user")
(OS user changed to postgres user)

About account creation

Questions I'm working on: When I googled for creating a PostgreSQL account, I found only articles that said "Create a PostgreSQL user account after installing PostgreSQL". The method of the textbook is to "create a user account before installation". I think it's more natural to "install first and then create a user account". Why do textbooks create an account first? Solution: The user set this time is a user managed by the OS (CentOS7). The user set after installation is a user managed by PostgreSQL, which is middleware. Be careful not to confuse the two users! (Reference: "User and password settings") The document says "PostgreSQL can be installed without the need for non-privileged users, that is, superuser (root) privileges. (Citation: PostgreSQL 8.2.6 documentation)" Did not understand the meaning of this sentence. However, since I finally switched to a general user in this work, the source code after that will be acquired by a general user (= non-privileged user). Somehow, I understood the meaning.

CentOS7 usage memo

How to switch users

** ● $ (general user) → # (root user) **

$ su -

The Password required to switch is vagrant

in conclusion

-You have now created a CentOS 7 user account that operates PostgreSQL.

・ It took 16 days (except New Year's Eve from 12/23 (Wednesday) to 1/8 (Friday)) to execute the 4-line command written in the textbook. Working time is 0.5 to 3 hours/day, which varies from day to day. Most of that time was google time.

-Since I don't have basic knowledge of commands, it took me a long time to read what was written in each article while I was investigating. I also spent a lot of time reading articles that weren't directly related to executing this command. I thought that the speed of information gathering would be increased by gaining peripheral knowledge while investigating in that way and being able to quickly determine whether or not the article contains what is needed at that time. .. It takes a lot of time now ...

・ Since my question and what I want to investigate are unclear, I had the feeling of clarifying the outline of my question while investigating. I don't know too much now, and it takes time to clarify this "what is the question". In this case, for example, the contents summarized in "About account creation". Since I didn't know that there are two types of users, OS users and PostgreSQL users, the prerequisites for google were not set, and it took time to answer the question. If we knew that there were two types of users, we could have narrowed down the information from the beginning. Isn't the precondition wrong while investigating? I was suspicious of myself, and while I was investigating further with that suspicion, I wonder if there were two types! I noticed that. In order to solve the problem, it is necessary to clarify "what I do not understand" as soon as possible, and I felt again that knowledge is necessary for that purpose.

・ Next is the second preparation. Proceed to "Installing the various software packages required to install PostgreSQL from source code". Preparations are still going on. I want to get into the main subject soon!

Unsolved issues

-Does the "-" in "# su --postgres" mean "root"? When I looked up "$ su-", it should have been ""-"means the root user".

-When building CentOS 7 with vagrant, at what stage was the root user & general user user name & password set?

reference

・ Books: "OSS textbook OSS-DB Silver Ver2.0 compatible" -Documentation: PostgreSQL 8.2.6 Document ・ "Differences between Unix and Linux that can be explained to people in 3 minutes"-Engineer's entrance-2020/10/12 ・ "What is GNU"-IT Glossary e-Words-2018/07/30 Updated article ・ "Introduction to GNU Make for Automation"-Oblove-2003/01/05 ・ "[For complete beginners] What is a Linux root user?"-Engineer's entrance-2018/08/30 ・ "[Even beginners can understand!] What is a Linux user?"-Engineer's entrance-2016/12/09 -"[Beginner] What is a Linux command? And how to display the command line"-Engineer's entrance-2018/08/30 ・ "Su --Become a command root user"-Professor Tokyo -"Notes on command line options for Linux systems"-Qiita-@rubytomato @ github's article -"[Useradd] command-create a new user"-2018/11/02 article -"Linux command [useradd] Create new user" -"User and password settings"-Introduction to DB starting with PostgreSQL — 2016/05/07 ・ "Build a development environment on Windows quickly with Vagrant + VirtualBox"-Qiita-@ ozawan's article

Recommended Posts

Environment construction for OSS-DB Silver # 2_Preparation 1-Create a user account to operate PostgreSQL on CentOS7
Let's install Docker on Windows 10 and create a verification environment for CentOS 8!
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
Let's create a gcloud development environment on a centos8 container
Create a user with an empty password on CentOS7
How to install GNOME as a desktop environment on CentOS 7
I tried to create a Spring MVC development environment on Mac
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
How to build a Ruby on Rails environment using Docker (for Docker beginners)
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
How to create a Maven repository for 2020
Build a development environment to create Ruby on Jets + React apps with Docker
Build a development environment for Docker + Rails6 + Postgresql
How to create a database for H2 Database anywhere
[Memo] Create a CentOS 8 environment easily with Docker
[Rails] rails new to create a database with PostgreSQL
Building a Ruby environment for classes on Mac
How to create pagination for a "kaminari" array
How to build a Pytorch environment on Ubuntu
Introduction to kotlin for iOS developers ①-Environment construction
Create a docker environment for Oracle 11g XE
Create an environment for Tomcat and Postgres on a Chromebook without using developer mode
Adding a network device to CentOS 8 on Hyper-V does not automatically create a virtual network interface