Details of the steps to install the Postgresql database in Ubuntu

  • 2020-05-06 11:56:55
  • OfStack


As we all know, PostgreSQL is a free object-relational database server (DBMS). Installing Postgresql under ubuntu is very easy.

The installation method of is as follows:

1. Install Postgresql server and client :

sudo apt-get install postgresql postgresql-client

2. Generally, Postgresql has been started after installation. The following are some common operating instructions:

#  Check the status 
sudo /etc/init.d/postgresql status

#  Start the 
sudo /etc/init.d/postgresql start

#  stop 
sudo /etc/init.d/postgresql stop

#  restart 
sudo /etc/init.d/postgresql restart

3. Create a new database user root and specify as super user:

sudo -u postgres createuser --superuser root

4. Set root user's password:

sudo -u postgres psql
\password root

5. Create a database mydb:

sudo -u postgres createdb -O root test

6. Rename the database:

alter database mydb rename to mynewdb;

7. Login database:

psql -U root -d test -h -p 5432

-U specifies user, -d specifies database, -h specifies server, -p specifies port.

8. Common console commands :

\h: view an explanation of the SQL command, such as \h select. \ & # 63; : view the list of psql commands. \l: list all databases. \c [database_name] : connect to other databases. \d: lists all the tables for the current database. \d [table_name] : lists the structure of a table. \du: list all users. \e: open a text editor. \conninfo: lists the current database and connection information.

9. Database operation:

Database operations are normal SQL, but there is PostgreSQL's own syntax, as detailed in the documentation.

Create table:

create table users (
 id serial primary key,
 username varchar(20),
 password varchar(20)

Insert data:

insert into users(username, password) values('admin', 'admin');

Query data

select * from users;

10, install the graphical interface client PgAdmin, who still use sql table, after all, time is life:

#  Check the status 
sudo /etc/init.d/postgresql status

#  Start the 
sudo /etc/init.d/postgresql start

#  stop 
sudo /etc/init.d/postgresql stop

#  restart 
sudo /etc/init.d/postgresql restart

Note: part of the content of this article is from online information, if there is infringement, please inform.


Related articles: