DEV Community

Vincent Tommi
Vincent Tommi

Posted on

Moving from Ubuntu to Windows? Here's How to Create a PostgreSQL Database Using CMD

If you've been developing on Ubuntu Linux and recently moved to Windows, you might have noticed that some familiar terminal commands and workflows are different.

When working on Ubuntu, I'm used to opening the terminal, connecting to PostgreSQL, creating databases, managing users, and running commands without thinking too much about the environment.

However, after moving to Windows, some of these operations can feel unfamiliar, especially when PostgreSQL commands don't work the same way you expect.

In this article, I'll walk through how to connect to PostgreSQL, create a database, and manage privileges using Windows Command Prompt (CMD).

The goal is to make the transition easier for developers coming from Ubuntu.

1. Connecting to PostgreSQL from Windows CMD

On Ubuntu, you might be familiar with connecting to PostgreSQL using:

sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

Or:

psql -U postgres
Enter fullscreen mode Exit fullscreen mode

On Windows, open Command Prompt (CMD) and use:

psql -U postgres -h localhost -p 5432
Enter fullscreen mode Exit fullscreen mode

Let's break down the command:

  • psql: Opens the PostgreSQL interactive terminal.
  • -U postgres: Specifies the PostgreSQL username.
  • -h localhost: Connects to PostgreSQL running on your local machine.
  • -p 5432: Specifies the PostgreSQL port.

You'll be prompted to enter your password:

Password for user postgres:
Enter fullscreen mode Exit fullscreen mode

After successful authentication, you should see:

postgres=#
Enter fullscreen mode Exit fullscreen mode

You're now connected to PostgreSQL.

A small difference to remember: Unlike Ubuntu, Windows doesn't use sudo -u postgres to switch to the PostgreSQL operating-system account. Instead, you authenticate using PostgreSQL credentials with -U postgres.

2. Creating a Database

Once connected, creating a database is almost identical on both operating systems.

Let's create a database called student_management.

CREATE DATABASE student_management;
Enter fullscreen mode Exit fullscreen mode

Expected output:

CREATE DATABASE
Enter fullscreen mode Exit fullscreen mode

Your database has been created successfully.

3. Granting Database Privileges

If you're working with PostgreSQL users and permissions, you can grant privileges using:

GRANT ALL PRIVILEGES ON DATABASE student_management TO postgres;
Enter fullscreen mode Exit fullscreen mode

Expected output:

GRANT
Enter fullscreen mode Exit fullscreen mode

Remember that postgres is normally a PostgreSQL superuser, so it already has unrestricted privileges. We're using this command to understand how privilege management works.

4. Connecting to Your Newly Created Database

In Ubuntu, you might be used to switching databases using:

psql -d student_management
Enter fullscreen mode Exit fullscreen mode

When you're already inside the PostgreSQL interactive terminal on Windows, you can simply execute:

\c student_management
Enter fullscreen mode Exit fullscreen mode

Expected output:

You are now connected to database "student_management" as user "postgres".
Enter fullscreen mode Exit fullscreen mode

Your prompt should change to:

student_management=#
Enter fullscreen mode Exit fullscreen mode

You're now working inside your new database.

5. Granting Schema and Table Privileges

PostgreSQL also allows you to manage permissions at the schema and table levels.

Grant privileges on the public schema:

GRANT ALL PRIVILEGES ON SCHEMA public TO postgres;
Enter fullscreen mode Exit fullscreen mode

Grant privileges on existing tables:

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgres;
Enter fullscreen mode Exit fullscreen mode

Grant privileges on existing sequences:

GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO postgres;
Enter fullscreen mode Exit fullscreen mode

These commands are useful when configuring databases for backend applications.

6. Useful PostgreSQL Commands for Ubuntu Developers Moving to Windows

One thing I appreciate about PostgreSQL is that most SQL commands and psql meta-commands remain the same regardless of whether you're using Ubuntu or Windows.

Here are some commands worth remembering:

Task PostgreSQL command
Connect to PostgreSQL psql -U postgres -h localhost -p 5432
List databases \l
Connect to a database \c database_name
List users and roles \du
List tables \dt
List schemas \dn
Check connection details \conninfo
Exit PostgreSQL \q

Important: Commands beginning with \ are PostgreSQL interactive terminal commands. They work inside psql, not directly in Windows CMD.

7. Complete Practical Example

Here is everything put together.

First, open Windows CMD:

psql -U postgres -h localhost -p 5432
Enter fullscreen mode Exit fullscreen mode

After entering your password, execute:

-- Create database
CREATE DATABASE student_management;

-- Grant database privileges
GRANT ALL PRIVILEGES ON DATABASE student_management TO postgres;

-- Connect to the database
\c student_management

-- Grant schema privileges
GRANT ALL PRIVILEGES ON SCHEMA public TO postgres;

-- Grant privileges on existing tables
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgres;

-- Grant privileges on existing sequences
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO postgres;

-- Verify current database
SELECT current_database();

-- Verify current user
SELECT current_user;

-- List databases
\l
Enter fullscreen mode Exit fullscreen mode

8. A Few Things to Remember When Moving from Ubuntu to Windows

Here are some differences that might save you time:

  1. Terminal differences: Ubuntu uses Bash by default, while Windows CMD uses a different command syntax. PowerShell is another option on Windows.

  2. PostgreSQL installation paths: On Windows, PostgreSQL is commonly installed under C:\Program Files\PostgreSQL\. You may have to configure the bin directory in your Windows PATH to run psql from any CMD location.

  3. Authentication: Ubuntu often uses operating-system user authentication through sudo -u postgres. Windows commonly uses password authentication with -U postgres.

  4. PostgreSQL commands: SQL statements and psql commands such as \l, \dt, and \du remain the same.

  5. Ports: PostgreSQL commonly uses port 5432 on both operating systems, unless you've configured a different port.

Conclusion

Moving from Ubuntu to Windows doesn't mean starting your development journey all over again. Most of the tools and technologies remain the same; what changes is how you interact with the operating system.

For developers migrating from Linux, my advice is to learn the Windows terminal equivalents gradually instead of assuming every Ubuntu command will work in CMD.

I'm documenting these differences as I continue adjusting my development workflow on Windows.

Let's discuss:

For developers who have moved from Ubuntu or another Linux distribution to Windows:

  • What was the most challenging command or development workflow to adapt?
  • Do you prefer Windows CMD, PowerShell, or Git Bash for backend development?

Share your experiences. They might help another developer making the same transition.

Top comments (0)