Connecting to PostgreSQL using the command line
PostgreSQL provides a powerful interactive terminal called psql that allows you to connect to databases, run queries, and manage your data directly from the command line. On ruachost.com, you can use this method to quickly access and administer your PostgreSQL databases.
Why Use the Command Line?
Section titled “Why Use the Command Line?”- Fast and direct access to your database.
- Ability to run SQL queries and administrative commands.
- Useful for scripting and automation.
- Essential for troubleshooting and advanced database management.
Steps to Connect to PostgreSQL Using psql
Section titled “Steps to Connect to PostgreSQL Using psql”Step 1: Log in via SSH
Section titled “Step 1: Log in via SSH”- Use SSH to connect to your hosting account:
ssh username@yourserver.ruachost.comStep 2: Run the psql Command
Section titled “Step 2: Run the psql Command”-
At the command line, type:
Terminal window psql dbname username -
Replace
dbnamewith the name of your database. -
Replace
usernamewith your PostgreSQL username.
Step 3: Enter Password
Section titled “Step 3: Enter Password”- At the password prompt, type the database user’s password.
- If correct, the psql prompt will appear.
Common psql Commands
Section titled “Common psql Commands”-
View help for psql commands
Section titled “View help for psql commands”Terminal window \? -
View help for SQL commands
Section titled “View help for SQL commands”Terminal window \h -
Show current database connection info
Section titled “Show current database connection info”Terminal window \conninfo -
List tables and owners
Section titled “List tables and owners”Terminal window \dt -
List all tables, views, and sequences
Section titled “List all tables, views, and sequences”Terminal window \z -
Exit psql
Section titled “Exit psql”Terminal window \q