I have experienced the process of installing PostgreSQL on a Linux system and using Navicat to remotely connect to PostgreSQL in a Windows environment after successful installation.
The first time is to install PostgreSQL in a virtual machine environment installed on Windows. The Linux version used by the virtual machine is CentOS 7.
The second time was to install PostgreSQL on a purchased host across the ocean. The Linux version used by the host was also CentOS 7.
The process of installing PostgreSQL for the second time is recorded below.
Table of contents
1. Check the Linux version and processor type
2. Visit the PostgreSQL official website: PostgreSQL: Linux downloads (other)
3. Select the specific PostgreSQL version to be installed
4. Select platform (Linux version)
5. Select architecture (Linux processor type)
6. Copy, paste and run commands
7. Check PostgreSQL startup status
8. You can try restarting the service again
2. Configuration related files
2. Configure the secret key method
4. Check the service startup status
3. Log in to the database and test it (create user and database)
2. Switch to user postgres (use the built-in user postgres)
5. Bind a subordinate role to the account (super user)
4. Import the script into the PostgreSQL database and create a data table
1. Prepare .sql source file in Windows system
2. Copy the .sql file to Linux
3. Import the .sql file into the database
4. Check whether the table has been created
1. Installation steps
1. Check the Linux version and processor type
cat /etc/centos-release (show release version)
uname -m (display processor type)
2. Visit the PostgreSQL official website:PostgreSQL: Linux downloads (other)
Make your selection based on what you know about your Linux version.
3. Select the specific PostgreSQL version to be installed
Note: Currently, yum is used for installation. Please configure the yum source in advance.
4. Select platform (Linux version)
5. Select architecture (Linux processor type)
6. Copy, paste and run commands
Copy the commands in the image below to install the repository RPM, install PostgreSQL, (optionally) initialize the database and enable autostart.
(1) Copy the command to the Linux environment and run the command to install RPM
(2) Copy the command to the Linux environment and run the command to install PostgreSQL (the process is too long, so I took 2 screenshots)
(3) Copy the command to the Linux environment, optionally initializing the database and enabling automatic startup.
7. Check PostgreSQL startup status
systemctl status postgresql-14
Note: ① The number 14 needs to be changed according to the installed PostgreSQL version. If the number at the end of the command does not match the installed version, an error will be reported, such as: systemctl status postgresql-13, an error will be reported: Unit postgresql-13.service could not be found;
② Adding sudo before this command does not affect the execution of the command. The sudo command executes the command as the system administrator.
8. You can try restarting the service again
systemctl restart postgresql-14.service
2. Configuration related files
1. Configure remote login
Modify the /var/lib/pqsql/14/data/postgresql.conf file in this directory.
input the command:
vi /var/lib/pgsql/14/data/postgresql.conf
Note: You need to master the use of vi Linux vi/vim | Newbie tutorial
Uncomment listen_addresses and change the value to "*".
2. Configure the secret key method
Modify the /var/lib/pgsql/14/data/pg_hba.conf file in this directory.
input the command:
vi /var/lib/pgsql/14/data/pg_hba.conf
Find IPv4 local connections and add the content in the red box below the existing configuration.
3. Restart the service
systemctl restart postgresql-14.service
4. Check the service startup status
Here, PostgreSQL is installed~
At this time, there is a built-in super user postgres in the database. Postgres can create a new super user in the database, or turn a super user into an ordinary user...
There is so much knowledge to learn. You can learn about it yourself on Baidu's "PostgreSQL roles, users, permissions, database security, etc."~~
3. Log in to the database and test it (create user and database)
1. Log in to the database
su - postgres
Note: Learn the su command while practicing as a reference Linux su command | Novice tutorial
2. Switch to user postgres (use the built-in user postgres)
psql -U postgres
Note: In the following content, PostgreSQL related commands will be used, such as: psql...
3. Create users and databases
Create root user and set password:
create user root with password '密码';
Create database:
create database 库名 owner root;
Authorization:
grant all privileges on database 库名 to root;
Query an existing database:
\l
Note: l is the lowercase letter L
4. Query role attributes
\du
5. Bind a subordinate role to the account (super user)
GRANT postgres TO root;
\du
Query character attributes again
6. Log out of postgres
\q
7.Exit the current shell
exit
Note: exit is a Linux command
4. Import the script into the PostgreSQL database and create a data table
1. Prepare .sql source file in Windows system
2. Copy the .sql file to Linux
The copy process is omitted: I use the FinalShell tool and drag it directly to the specified directory.
Enter the directory where the .sql source file of the Linux system is located and enter the command:
ls -l
First look at the file attributes and other information (the purpose is to check whether the file is copied to the Linux system).
3. Import the .sql file into the database
Use the command to import the "setup.sql" file just now into the database just created in PostgreSQL.
psql -U root -d 库名 -f /usr/local/go-web/SQL/setup.sql
Note: ① root is the user just created, that is, import the database to this user; ② The database name refers to which database, such as the database created in the above process; ③ /usr/local/go-web/SQL/setup. sql is the path to the location of the .sql source file.
4. Check whether the table has been created
input the command:
su - postgres
input the command:
psql -h localhost -p 5432 -U root 库名
After entering the password, you will enter the database.
Enter the command under the database:
\dt
Display the table created by just importing the .sql source file.
5. Remotely connect to PostgreSQL through Navicat
1. Install Navicat in Windows environment
Skip the installation and cracking process...
2. Run Navicat to connect to PostgreSQL on the remote host
SSH configuration (Note: ① In this step, you can click Test Connection, and there will be a prompt of success. Of course, there are other situations; ② Or use telnet IP 22 in the Windows CMD window to test whether port 22 of the Linux host is available)
General configuration
After clicking OK, open the connection and you can see the library and table.
This concludes the process recording of installing PostgreSQL in the Linux environment and remote connection in the Windows environment~