PostgreSQL includes powerful command‑line tools that allow administrators to create, modify, and delete databases and users. On ruachost.com, you can manage PostgreSQL directly from the terminal for advanced control and automation.
Why Use the Command Line?
-
Faster than using a web interface.
-
Enables scripting and automation.
-
Provides full access to PostgreSQL features.
-
Essential for developers and system administrators.
Creating PostgreSQL Users
-
Log in to your server via SSH.
-
Switch to the PostgreSQL superuser:
Bash su - postgres -
Create a new user interactively:
Bash createuser --interactive --pwprompt-
Enter the username.
-
Enter and confirm the password.
-
Choose whether the user is a superuser.
-
Choose whether the user can create databases.
-
Choose whether the user can create other roles.
-
Creating PostgreSQL Databases
-
Log in as the PostgreSQL superuser:
Bash su - postgres -
Create a database owned by a specific user:
Bash createdb -O username dbname-
Replace
usernamewith the database owner. -
Replace
dbnamewith the database name.
-
???? Tip: Users with permission to create databases can run:
Bash
createdb dbname
Granting User Privileges
-
Connect to PostgreSQL:
Bash psql -U postgres -
Grant privileges:
GRANT ALL PRIVILEGES ON DATABASE dbname TO username;-
Replace
dbnamewith the database name. -
Replace
usernamewith the user.
-
Deleting PostgreSQL Databases
-
Log in as the PostgreSQL superuser.
-
Run:
Bash dropdb dbname⚠️ Warning: This deletes the database and all its data immediately without confirmation.
Deleting PostgreSQL Users
-
Log in as the PostgreSQL superuser.
-
Run:
Bash dropuser username⚠️ Important: You cannot drop a user if they own any databases or objects.
-
First, reassign ownership or drop the database.
-
Important Notes
|