Use python to operate postgresSQL under ubuntu
This article records the process of using python to operate the postgresSQL database under the ubuntu system, the problems encountered and the solutions.
System version: Ubuntu 20.04
Date: March 2021
Database version: 12
1. Install postgresSQL
According to the tutorial on the official website , execute the following command in the terminal, and version 12 is selected here.
# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update the package lists:
sudo apt-get update
# Install the latest version of PostgreSQL.
sudo apt-get -y install postgresql-12
Then start the service:
sudo pg_ctlcluster 12 main start
2. Create a database
To create a database, you first need to switch users with the following command:
sudo -i -u postgres
Then create the database:
createdb mydb
enter the database
psql mydb