Logging in Using pgAdmin
- Open pgAdmin 4.
- If this is your first time, set a master password to secure pgAdmin.
- In the left panel, right-click “Servers” and select “Create” > “Server…” to register a new server.
- Enter a name for the server on the “General” tab.
- Go to the “Connection” tab and enter:
- Host: Usually “localhost” or “127.0.0.1”
- Port: Default is 5432
- Maintenance database: Usually “postgres”
- Username: Usually “postgres”
- Password: The password set for the “postgres” user when PostgreSQL was installed or later changed.
- Click “Save”.
- Double-click the server name in the left panel, enter the password if prompted, and you will be logged in and connected to the database server.
Logging in Using psql Command Line
- Open your terminal or command prompt.
- Type the command:
psql -h localhost -U postgres -d postgres - When prompted, enter the password for the “postgres” user.
- You will get a PostgreSQL prompt where you can run SQL queries.
Commands
List all databases
postgres=# \l
Connect to the database named postgres
postgres=# \c postgres
Disconnect
postgres=# \q
postgres=# \!
Create database
CREATE DATABASE <database_name> WITH OWNER <username>;
Drop database
DROP DATABASE IF EXISTS <database_name>;
Rename database
ALTER DATABASE <old_name> RENAME TO <new_name>;