Inspection before installation
First check to see if an older version is already installed:
dpkg -l |grep postgresql
If a certain version of postgresql is already installed, please uninstall it first.
Install postgresql
Add postgresql source:
sudo touch /etc/apt/sources.list.d/pgdb.list
sudo vim /etc/apt/sources.list.d/pgdb.list
Add the following line of data to the pgdb.list file:
deb https://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main
Execute the following command to add the key of the postgresql installation package:
sudo wget --quiet -O - https://postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Then you can install:
sudo apt-get update
sudo apt-get install postgresql-9.4
If everything goes well, you can see the following installation results:
OK, a total of five components are installed.
Create databases and roles
Next, we will learn about the basic usage of postgresql by creating a database and corresponding roles.
First switch the user to postgres (postgres is a system user created during the installation of postgresql, the role of this user is similar to sa in sqlserver):
sudo -i -u postgres
Create the database testdb:
createdb testdb
Next, create the role testuser, which we implement through the postgresql client:
psql
Type in the interactive command and hit enter:
CREATE USER testuser; // The role created by this command has login privileges by default
Set a password for testuser:
\password testuser // Follow the prompts to enter the password
Set the owner of the database testdb to testuser:
ALTER DATABASE testdb OWNER TO testuser;
Use the \q command to log out of the current postgresql login, and then use the following command to log in to the newly created database:
psql -d testdb -U testuser -h 127.0.0.1 -W
Enter the password you just set for testuser:
We see that the current database is already the testdb we created.
Inspection before installation
First check to see if an older version is already installed:
dpkg -l |grep postgresql
If a certain version of postgresql is already installed, please uninstall it first.
Install postgresql
Add postgresql source:
sudo touch /etc/apt/sources.list.d/pgdb.list
sudo vim /etc/apt/sources.list.d/pgdb.list
Add the following line of data to the pgdb.list file:
deb https://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main
Execute the following command to add the key of the postgresql installation package:
sudo wget --quiet -O - https://postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Then you can install:
sudo apt-get update
sudo apt-get install postgresql-9.4
If everything goes well, you can see the following installation results:
OK, a total of five components are installed.
Create databases and roles
Next, we will learn about the basic usage of postgresql by creating a database and corresponding roles.
First switch the user to postgres (postgres is a system user created during the installation of postgresql, the role of this user is similar to sa in sqlserver):
sudo -i -u postgres
Create the database testdb:
createdb testdb
Next, create the role testuser, which we implement through the postgresql client:
psql
Type in the interactive command and hit enter:
CREATE USER testuser; // The role created by this command has login privileges by default
Set a password for testuser:
\password testuser // Follow the prompts to enter the password
Set the owner of the database testdb to testuser:
ALTER DATABASE testdb OWNER TO testuser;
Use the \q command to log out of the current postgresql login, and then use the following command to log in to the newly created database:
psql -d testdb -U testuser -h 127.0.0.1 -W
Enter the password you just set for testuser:
We see that the current database is already the testdb we created.