How to get a md5 password for a user on PostgreSQL? 如何在PostgreSQL上为用户获取md5密码?

If you look through table pg_authid on PostgreSQL, you can find the values of column rolpassword are hashed strings. For example, a string like "md58d11c0b8417d3947cb544f1174fd44ce".

How to get rolpassword? The formula is:

"md5" + md5(password + username)

Here are several ways you can create one, where the username is "admin" and the password is "password123"...

PostgreSQL:

# select 'md5' || md5('password123' || 'admin')md53f84a3c26198d9b94054ca7a3839366d

Linux:

# echo -n "md5"; echo -n "password123admin" | md5sum | awk '{print $1}'
md53f84a3c26198d9b94054ca7a3839366d

NOTE: The -n is critical to avoid including the newline character in your hash!

MacOS:

➜ echo -n "md5"; md5 -qs "password123admin"                                                                                                                                                                                   
md53f84a3c26198d9b94054ca7a3839366d

Python 2:

>>> import hashlib
>>> print("md5" + hashlib.md5("password123" + "admin").hexdigest())
md53f84a3c26198d9b94054ca7a3839366d

Python 3:

as above, but use binary strings

>>> import hashlib
print("md5" + hashlib.md5(b"password123" + b"admin").hexdigest())

 

Reference

1. RCross. Answer: Postgres: MD5 Password / Plain password. Nov 21 '19 at 16:40

 

### Python Project Simple CRUD Operations Example For implementing simple Create, Read, Update, and Delete (CRUD) operations within a Python project using Django framework, one can follow this structured approach: #### Creating a New Django Project To initiate a new Django project named `CRUD`, execute the provided command which sets up necessary directories and files required for starting development work[^1]. ```bash django-admin startproject CRUD ``` However, since specific interest lies in performing CRUD operations with more flexibility regarding database interactions outside of Django’s ORM but still within Python, utilizing SQLAlchemy provides extensive capabilities. #### Implementing CRUD Using SQLAlchemy ##### Setup Environment Ensure having installed both SQLAlchemy and an appropriate database driver such as psycopg2 for PostgreSQL or pymysql for MySQL. Installation via pip would look like: ```bash pip install sqlalchemy psycopg2-binary # For PostgreSQL # OR pip install sqlalchemy pymysql # For MySQL ``` ##### Define Models Define models according to schema requirements; here simplified examples are given without specifying table names explicitly through metadata parameters. ```python from sqlalchemy import Column, Integer, String, ForeignKey from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship, sessionmaker import sqlalchemy Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String(50)) age = Column(Integer) class Post(Base): __tablename__ = 'posts' id = Column(Integer, primary_key=True) title = Column(String(100)) content = Column(String()) user_id = Column(Integer, ForeignKey('users.id')) author = relationship("User", back_populates="posts") User.posts = relationship("Post", order_by=Post.id, back_populates="author") ``` ##### Perform CRUD Actions With defined models, perform actions similar to those shown below where creation, modification, addition, querying, updating, and deletion processes occur within sessions managed by SQLAlchemy[^2]. ```python engine = sqlalchemy.create_engine('postgresql://username:password@localhost/mydatabase') Session = sessionmaker(bind=engine) session = Session() # Create new objects new_user = User(name='Bob', age=30) new_post = Post(title='Hello World!', content='This is my first post.', author=new_user) # Add objects to the session session.add(new_user) session.add(new_post) # Commit transaction session.commit() # Modify an existing object existing_user = session.query(User).filter_by(name='Alice').first() if existing_user: existing_user.age = 25 session.commit() # Query data all_users = session.query(User).all() for user in all_users: print(f'{user.name} ({user.age})') # Deleting records specific_post_to_delete = session.query(Post).get(post_id) if specific_post_to_delete: session.delete(specific_post_to_delete) session.commit() ``` --related questions-- 1. How does defining relationships between tables impact performance when executing complex queries? 2. What advantages do Object Relational Mappers offer over traditional SQL statements execution methods? 3. Can you provide guidance on best practices while designing relational databases schemas intended for web applications? 4. In what scenarios might direct SQL usage be preferred over employing an ORM tool like SQLAlchemy?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值