The psql utility lets you interact with PostgreSQL databases directly from the terminal. You can run queries, manage database objects and execute administrative commands without needing a graphical interface.
You will connect to PostgreSQL using SSH, authenticate with your database credentials and run SQL commands through the psql prompt. This method works for both local and remote database connections.
You will establish an SSH connection to your server and then use psql to access your PostgreSQL database. The connection method varies depending on whether you are using the default postgres superuser or a specific database user.
dbname with your database name and user with your PostgreSQL username:sudo -u postgres psql dbname
If you need to access PostgreSQL as the default postgres superuser without specifying a database, switch to the postgres user first and then run psql.
You should confirm which database you are connected to before running queries. This prevents accidental changes to the wrong database.
\conninfo
\c database_namedatabase_name with the target database. The prompt updates to reflect your new connection.Once connected, you can execute SQL queries and use psql meta-commands to inspect your database structure. Meta-commands start with a backslash and do not require a semicolon.
View all tables in your current database:
\dt
Examine the structure of a specific table:
\d table_name
Retrieve all rows from a table:
SELECT * FROM table_name;
Get help with SQL syntax:
\h
View available psql commands:
\?
Exit psql and return to your shell:
\q
You can now connect to PostgreSQL databases using psql and execute SQL commands from the terminal. You established an SSH connection, authenticated with your database credentials and verified your connection details.
If you need to import data into your PostgreSQL database, you can import database files via command line using similar principles. For users working with multiple database systems, you can also connect to MySQL with SSH using comparable connection methods. Our VPS hosting includes full root access for database management.
Get scalable resources with our VPS hosting with root access and optional software.
Get VPS HostingPerfect for websites and small businesses unlimited bandwidth with cPanel hosting.
Get cPanel Hosting