DEV Community

Cover image for How to Connect to PostgreSQL and Create a Database, User, and Tables
Navas Herbert
Navas Herbert

Posted on

3 1 1 1 1

How to Connect to PostgreSQL and Create a Database, User, and Tables

PostgreSQL is a powerful open-source relational database system that's popular for web applications, data analytics, and more. In this guide, I'll walk you through connecting to a PostgreSQL server, creating a database and user, setting up tables, and connecting via DBeaver.

Prerequisites

  • Access to a Linux server with PostgreSQL installed
  • SSH client on your local machine
  • Basic command line knowledge

Step 1: Connect to Your Server via SSH

First, connect to your remote server using SSH:

ssh navas@172.184.XXX.XXX
Enter fullscreen mode Exit fullscreen mode

Enter your password when prompted. Once logged in, you'll need to access the PostgreSQL command line interface.

Step 2: Access the PostgreSQL CLI

PostgreSQL creates a default postgres user during installation. Switch to this user:

sudo -i -u postgres
Enter fullscreen mode Exit fullscreen mode

Now access the PostgreSQL interactive terminal:

psql
Enter fullscreen mode Exit fullscreen mode

You should now see the PostgreSQL prompt: postgres=#

Step 3: Create a New User

Let's create a dedicated user for your database operations:

CREATE USER navas WITH PASSWORD 'your_secure_password';
Enter fullscreen mode Exit fullscreen mode

For development purposes, you might want to grant superuser privileges:

ALTER USER navas WITH SUPERUSER;
Enter fullscreen mode Exit fullscreen mode
  • Note: In production, grant only the necessary privileges following the principle of least privilege.

Step 4: Create a Database

Create a new database owned by your user:

CREATE DATABASE navasdb OWNER navas;
Enter fullscreen mode Exit fullscreen mode

Step 5: Connect to Your New Database

Connect to your newly created database:

\c navasdb
Enter fullscreen mode Exit fullscreen mode

Step 6: Create Tables

Now let's create a sample table. Here's an example users table:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(255) NOT NULL,
);
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify Your Setup

Check the existing tables in your database:

\dt
Enter fullscreen mode Exit fullscreen mode

View the schemas:

\dn
Enter fullscreen mode Exit fullscreen mode

Step 8: Exit PostgreSQL

When you're done, exit the PostgreSQL CLI:

\q
Enter fullscreen mode Exit fullscreen mode

Then exit the postgres user session:

exit
Enter fullscreen mode Exit fullscreen mode

Step 9: Restart PostgreSQL (If Needed)

If you've made configuration changes that require a restart:

sudo systemctl restart postgresql
Enter fullscreen mode Exit fullscreen mode

Connecting with DBeaver

DBeaver is a popular database GUI tool. Here's how to connect to your PostgreSQL database:

  1. Install DBeaver if you haven't already (available at dbeaver.io)
  2. Open DBeaver and click on "New Database Connection"
  3. Select PostgreSQL from the database list
  4. Enter connection details:
    • Host: 172.184.XXX.XXX
    • Port: 5432 (default PostgreSQL port)
    • Database: navasdb
    • Username: navas
    • Password: your_secure_password
  5. Test Connection to verify everything works
  6. Click Finish to save the connection

Troubleshooting Tips

If you encounter connection issues:

  • Verify PostgreSQL is running:
  sudo systemctl status postgresql
Enter fullscreen mode Exit fullscreen mode

Conclusion

You've now successfully:

  • Connected to your PostgreSQL server
  • Created a new database user
  • Established a new database
  • Created tables
  • Connected via DBeaver for graphical management

This setup gives you a solid foundation for developing applications with PostgreSQL.

Remember to always:

  • Use secure passwords
  • Follow proper privilege management in production environments
  • Regularly backup your databases

Happy databasing!

Neon image

Serverless Postgres in 300ms (❗️)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (2)

Collapse
 
ngare_dancan profile image
Dancan Ngare

Great article. I'm keeping this for future reference.

Collapse
 
emmanuel_kiriinya_416fc40 profile image
Emmanuel Kiriinya

Saving this as my cheatsheet

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay