Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PostgreSQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use PostgreSQL". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use PostgreSQL.

PostgreSQL is a free object-relational database server (ORDBMS), distributed under a flexible BSD license, which PostgreSQL developers pronounce as post-gress-Q-L, because of the flexibility of the license, anyone can use, modify and distribute PostgreSQL for any purpose free of charge.

I. installation

First install the PostgreSQL client:

Sudo apt-get install postgresql-client

Then, install the PostgreSQL server:

Sudo apt-get install postgresql

Normally, after the installation is completed, the PostgreSQL server will automatically be turned on on port 5432 of this machine. If you also want to install the graphical management interface, you can run the following command:

Sudo apt-get install pgadmin3 II. Add new users and new databases

After the initial installation, a database named postgres and a database user named postgres are generated by default. It is important to note that a Linux system user named postgres is also generated. Next, we use postgres users to generate other users and new databases. There are several ways to achieve this, and here are two.

The first is to use the PostgreSQL console. First, create a new Linux user with the name you want. Here is dbuser:

Sudo adduser dbuser

Then, switch to the postgres user.

Sudo su-postgres

Next, log in to the PostgreSQL console using the psql command:

Psql

At this time, it is equivalent to the system user postgres to log in to the database as a database user with the same name, which does not need to enter a password. If everything is all right, the system prompt will change to "postgres=#", indicating that you have entered the database console. The following commands are done in the console. The first thing is to use the\ password command to set a password for the postgres user:

\ password postgres

The second thing is to create the database user dbuser (which you just created is the Linux system user) and set the password:

CREATE USER dbuser WITH PASSWORD 'password'

The third thing is to create the user database, in this case exampledb, and specify the owner as dbuser:

CREATE DATABASE exampledb OWNER dbuser

The fourth thing is to grant all permissions to the exampledb database to dbuser, otherwise dbuser can only log in to the console and does not have any database operation permissions:

GRANT ALL PRIVILEGES ON DATABASE exampledb to dbuser

Finally, exit the console using the\ Q command (you can also press ctrl+D directly).

\ Q

The second is to use the shell command line.

Adding new users and databases can be done not only in the PostgreSQL console, but also on the shell command line. This is because PostgreSQL provides command-line programs createuser and createdb, or take the new user dbuser and database exampledb as an example. First, create the database user dbuser and specify it as superuser:

Sudo-u postgres createuser-- superuser dbuser

Then, log in to the database console, set the password for the dbuser user, and exit the console when you are finished:

Sudo-u postgres psql\ password dbuser\ Q

Next, under the shell command line, create the database exampledb and specify the owner as dbuser:

Sudo-u postgres createdb-O dbuser exampledb III. Login database

After you add a new user and a new database, log in to the database as the new user, using the psql command.

Psql-U dbuser-d exampledb-h 127.0.0.1-p 5432

The parameters of the above command are as follows:-U specifies the user,-d specifies the database,-h specifies the server, and-p specifies the port. After entering the above command, you will be prompted for the password of the dbuser user. If you enter it correctly, you can log in to the console. The psql command has an abbreviated form, and if the current Linux system user is also a PostgreSQL user, the user name (part of the-U parameter) can be omitted. For example, if my Linux system user name is ruanyf and there is a user with the same name in the PostgreSQL database, then after I log in to the Linux system as ruanyf, I can log in to the database directly using the following command without a password.

Psql exampledb

At this point, if there is a database with the same name as the current system user inside the PostgreSQL, even the database name can be omitted. For example, suppose there is a database called ruanyf, and you can log in to it by typing psql directly.

Psql

In addition, if you want to recover external data, you can use the following command.

Psql exampledb IV. Console commands

In addition to the previously used\ password command (set password) and\ Q command (exit), the console provides a series of other commands.

\ h: check the explanation of the SQL command, such as\ h select. \?: view a list of psql commands. \ l: list all databases. \ C [database_name]: connect to other databases. \ d: lists all tables for the current database. Example:\ d [table_name]: list the structure of a table. \ du: list all users. \ e: open a text editor. \ conninfo: lists the current database and connection information. 5. Database operation

The basic database operation is to use the general SQL language: # to create a new table

CREATE TABLE usertbl (name VARCHAR (20), signupdate DATE)

# insert data

INSERT INTO usertbl (name, signupdate) VALUES ('Zhang San', '2013-12-22')

# Select record

SELECT * FROM user_tbl

# updating data

UPDATE user_tbl set name ='Li Si 'WHERE name =' Zhang San'

# Delete record

DELETE FROM user_tbl WHERE name ='Li Si'

# add a field

ALTER TABLE user_tbl ADD email VARCHAR (40)

# update structure

ALTER TABLE usertbl ALTER COLUMN signupdate SET NOT NULL

# rename field

ALTER TABLE usertbl RENAME COLUMN signupdate TO signup

# deleting a field

ALTER TABLE user_tbl DROP COLUMN email

# rename the form

ALTER TABLE usertbl RENAME TO backuptbl

# Delete form

DROP TABLE IF EXISTS backup_tbl;88 thank you for reading, the above is the content of "how to use PostgreSQL", after the study of this article, I believe you have a deeper understanding of how to use PostgreSQL, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report