Postgresql-shell command line

Postgresql-shell command line

Adding new users and new databases can be done in the shell command line in addition to the PostgreSQL console. This is because PostgreSQL provides command line programs createuser and createdb. Still take the newly created user dbuser and database exampledb as an example.

First, create the database user dbuser and designate it as a super user.

sudo -u postgres createuser --superuser dbuser

Then, log in to the database console, set the password of the dbuser user, and exit the console after completion.

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

Three, log in to the database

After adding a new user and a new database, you must log in to the database in the name of the new user. At this time, the psql command is used.

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

The meanings of 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, the system will prompt for the password of the dbuser user. If the input is correct, you can log in to the console.

The psql command exists in short form. If the current Linux system user is also a PostgreSQL user, you can omit the user name (the part of the -U parameter). For example, my Linux system user name is ruanyf, and there is a user with the same name in the PostgreSQL database, after logging 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 time, if there is a database with the same name as the current system user in PostgreSQL, even the database name can be omitted. For example, if there is a database called ruanyf, you can log in to the database by typing psql directly.

psql

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

psql exampledb < exampledb.sql

Four, console commands

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

  • \h: View the explanation of SQL commands, such as \h select.
  • \?: View the list of psql commands.
  • \l: List all databases.
  • \c [database_name]: Connect to other databases.
  • \d: List all tables in the current database.
  • \d [table_name]: List the structure of a certain table.
  • \du: List all users.
  • \e: Open a text editor.
  • \conninfo: List the current database and connection information.

Five, database operations

The basic database operation is to use the general SQL language.

# Create a new table
CREATE TABLE user_tbl(name VARCHAR(20), signup_date DATE);

# Insert data
INSERT INTO user_tbl(name, signup_date) VALUES('Zhang San', '2013-12-22');

# Select record
SELECT * FROM user_tbl;

# Update data
UPDATE user_tbl set name ='Li Si' WHERE name ='Zhang San';

# Delete record
DELETE FROM user_tbl WHERE name ='Li Si';

# Add column
ALTER TABLE user_tbl ADD email VARCHAR(40);

# Update structure
ALTER TABLE user_tbl ALTER COLUMN signup_date SET NOT NULL;

# Rename field
ALTER TABLE user_tbl RENAME COLUMN signup_date TO signup;

# Delete column
ALTER TABLE user_tbl DROP COLUMN email;

# Table renamed
ALTER TABLE user_tbl RENAME TO backup_tbl;

# Delete table
DROP TABLE IF EXISTS backup_tbl;

Guess you like

Origin blog.csdn.net/qq_37061368/article/details/113523380