[Python/Django] Summary of frequently used commands (3) <Operation of PostgreSQL>

background

Here are some frequently used commands when developing with Python/Django.

I would like to take this opportunity to thank you for solving the problem by borrowing the wisdom of our predecessors, and I am very sorry to say that I will summarize it here as my own memo.

environment

(Production environment)

(Development environment)

1. PostgreSQL operation

1-1. Checking the version

Terminal


$ psql --version

psql (PostgreSQL) 13.1

1-2. Login

Terminal


$ psql -U postgres

Password for user postgres:                 
psql (13.1)
"help"Get help with.

postgres=#

1-3. Create a database

Terminal


postgres=#create database <database name>;

CREATE DATABASE

1-4. Check the list of databases

Terminal


postgres=# \l
Database list
name|owner|encoding|Collation| Ctype(Conversion operator) |Access rights
---------------+----------+------------------+----------+-------------------+-----------------------
 postgres      | postgres | UTF8             | C        | C                 |
<DB name>| postgres | UTF8             | C        | C                 |
 template0     | postgres | UTF8             | C        | C                 | =c/postgres          +
               |          |                  |          |                   | postgres=CTc/postgres
 template1     | postgres | UTF8             | C        | C                 | =c/postgres          +
               |          |                  |          |                   | postgres=CTc/postgres
(4 lines)

postgres=#  

1-5. Check the data

1-5-1. Enter the Python virtual environment and change to the directory where manage.py exists.

1-5-2. Execute the following to set the environment variables for database user and password.

-(Premise) Project setting file:

settings.py


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '<Database name>',
        'USER': os.environ.get('DB_USER'),
        'PASSWORD': os.environ.get('DB_PASSWORD'),
        'HOST': '',
        'PORT': '',
    }
}

Terminal


(venv_<Project name>) $ set DB_USER=<User name>
(venv_<Project name>) $ set DB_PASSWORD=<Password>

1-5-3. Execute the following to enable database operations.

Terminal


(venv_<Project name>) $ python manage.py dbshell --settings <database name>.settings

<Database name>=#

1-5-4. Displaying the table list

Terminal


<Database name>=# \dt

1-5-5. View all records in a particular table

Terminal


<Database name>=# select *from <table name>;

1-5.6. Other

1-5-7. Log out

Terminal


<Database name>=# \q
Or
postgres=# \q

(Relation)

[Python/Django] Summary of frequently used commands (1) <Creating virtual environment, project, application> [Python/Django] Summary of frequently used commands (2) [Python/Django] Summary of frequently used commands (4) -Part 1- <Production operation: Amazon EC2 (Amazon Linux 2)> [Python/Django] Summary of frequently used commands (4) -Part 2- <Production operation: Amazon EC2 (Amazon Linux 2)>


(Editor's note)

We will strive to create an application while carefully checking what values ​​are stored in each table in the database.

Recommended Posts

[Python/Django] Summary of frequently used commands (3) <Operation of PostgreSQL>
[Python/Django] Summary of frequently used commands (2) <Installing packages>
[Anaconda3] Summary of frequently used commands
[Python/Django] Summary of frequently used commands (4) -Part 2- <Production operation: Amazon EC2 (Amazon Linux 2)>
[Python/Django] Summary of frequently used commands (4) -Part 1- <Production operation: Amazon EC2 (Amazon Linux 2)>
Summary of frequently used commands of django (beginner)
Summary of frequently used commands in matplotlib
Summary of frequently used commands (with petit commentary)
Selenium webdriver Summary of frequently used operation methods
[Linux] Frequently used Linux commands (file operation)
List of frequently used Linux commands
[Linux] Frequently used Linux commands (folder operation)
[Linux] Review of frequently used basic commands 2
Python + Selenium Frequently used operation method summary
[Linux] Review of frequently used basic commands
pyenv Frequently used commands
Frequently used tmux commands
Frequently used Linux commands
Frequently used Linux commands
Frequently used linux commands
Summary of frequently used Python arrays (for myself)
[Linux command] A memorandum of frequently used commands
Frequently used pip commands
A collection of commands frequently used in server management
Display a list of frequently used commands on Zsh
Frequently used subpackages of SciPy
Frequently used commands in virtualenv
8 Frequently Used Commands in Python Django
Linux Frequently Used Commands [Personal Memo]
Frequently used Linux commands (for beginners)
Summary of methods often used in pandas
[Linux] Summary of middleware version confirmation commands
[Linux] List of Linux commands used in practice
Summary of petit techniques for Linux commands
[Machine learning] List of frequently used packages
Correspondence summary of array operation of ruby and python
List of frequently used built-in functions and methods
Summary of what was used in 100 Pandas knocks (# 1 ~ # 32)
Summary of tools used in Command Line vol.8
Summary of tools used in Command Line vol.5
Summary of evaluation functions used in machine learning
Frequently used methods of Selenium and Beautiful Soup
Docker. Set frequently used commands to alias "with explanation"
Summary of Linux (UNIX) commands that appeared in Progate
Summary of Pandas methods used when extracting data [Python]
Comparison table of frequently used processes of Python and Clojure
[For beginners] Django Frequently used commands and reference collection