This article introduces the installation and basic usage of PostgreSQL for first-time users to get started. The following content is based on the Debian operating system, other operating systems really do not have the energy to take into account, but most of the content should be generally applicable.
1. Installation
First, install the PostgreSQL client.
sudo apt-get install postgresql-client
Then, install the PostgreSQL server.
sudo apt-get install postgresql
Under normal circumstances, after the installation is complete, the PostgreSQL server will be automatically opened on port 5432 of the local machine.
If you also want to install the graphical management interface, you can run the following command, but this article does not cover this aspect.
sudo apt-get install pgadmin3
2. Add new users and new databases
After initial installation, a database named postgres and a database user named postgres are generated by default. It should be noted here that a Linux system user named postgres is also generated.
Below, we use the postgres user to generate additional users and a new database. There are several ways to achieve this, two of which are described here.
The first method, using the PostgreSQL console.
First, create a new Linux user, you can take the name you want, here is dbuser.
sudo adduser dbuser
Then, switch to the postgres user.
sudo su - postgres
Next, log into the PostgreSQL console using the psql command.
psql
At this time, it is equivalent to the system user postgres logging in to the database as a database user with the same name, which does not require a password. If everything is normal, the system prompt will change to "postgres=#", indicating that you have entered the database console at this time. The following commands are all 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 a database user dbuser (the Linux system user just created) and set a password.
CREATE USER dbuser WITH PASSWORD 'password';
The third thing is to create a user database, here exampledb, and specify the owner as dbuser.
CREATE DATABASE exampledb OWNER dbuser;
The fourth thing is to give all permissions of the exampledb database to dbuser, otherwise dbuser can only log in to the console without any database operation permissions.
GRANT ALL PRIVILEGES ON DATABASE exampledb to dbuser;
Finally, use the \q command to exit the console (you can also press ctrl+D directly).
\q
The second method, using the shell command line.
Adding new users and new databases can be done from the shell command line in addition to the PostgreSQL console. This is because PostgreSQL provides the command line programs createuser and createdb. Take the new user dbuser and database exampledb as an example.
First, create the database user dbuser and designate it as superuser.
sudo -u postgres createuser --superuser dbuser
Then, log in to the database console, set the password of the dbuser user, and exit the console when finished.
sudo -u postgres psql
\password dbuser
\q
Next, on the shell command line, create the database exampledb and specify the owner as dbuser.
sudo -u postgres createdb -O dbuser exampledb
3. Log in to the database
After adding a new user and a new database, log in to the database under the name of the new user, using the psql command.
psql -U dbuser -d exampledb -h 127.0.0.1 -p 5432
The meaning of the parameters of the above command is 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. Enter correctly, you can log in to the console.
Shorthand forms exist for psql commands. If the current Linux system user is also a PostgreSQL user, the username (part of the -U parameter) can be omitted. For example, my Linux system user name is ruanyf, and there is a user with the same name in the PostgreSQL database. After I log in to the Linux system as ruanyf, I can directly use the following command to log in to the database without a password.
psql exampledb
At this point, 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, assuming that there is a database called ruanyf, you can log in to the database directly by typing psql.
psql
Also, if you want to restore external data, you can use the command below.
psql exampledb < exampledb.sql
Fourth, the console command
In addition to the \password command (set password) and \q command (exit) already used, the console provides a series of other commands.
- \h: View 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: List all tables in the current database.
- \d [table_name]: List the structure of a table.
- \du: List all users.
- \e: Open a text editor.
- \conninfo: List current database and connection information.
Five, database operation
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 records
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 field
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 field
ALTER TABLE user_tbl DROP COLUMN email;# Table rename
ALTER TABLE user_tbl RENAME TO backup_tbl;#
DROP TABLE IF EXISTS backup_tbl; DROP TABLE IF EXISTS backup_tbl;