This step-by-step guide will help you get started connecting your project in WSL to a database. Get started with MySQL, PostgreSQL, MongoDB, Redis, Microsoft SQL Server, or SQLite.
PrerequisitesSome popular choices for a database system include:
MySQL is an open-source SQL relational database, organizing data into one or more tables in which data types may be related to each other. It is vertically scalable, which means one ultimate machine will do the work for you. It is currently the most widely used of the four database systems.
PostgreSQL (sometimes referred to as Postgres) is also an open-source SQL relational database with an emphasis on extensibility and standards compliance. It can handle JSON now too, but it is generally better for structured data, vertical scaling, and ACID-compliant needs like eCommerce and financial transactions.
Microsoft SQL Server includes SQL Server on Windows, SQL Server on Linux, and SQL on Azure. These are also relational database management systems set up on servers with primary function of storing and retrieving data as requested by software applications.
SQLite is an open-source self-contained, file-based, âserverlessâ database, known for its portability, reliability, and good performance even in low-memory environments.
MongoDB is an open-source NoSQL document database designed to work with JSON and store schema-free data. It is horizontally scalable, which means multiple smaller machines will do the work for you. It's good for flexibility and unstructured data, and caching real-time analytics.
Redis is an open-source NoSQL in-memory data structure store. It uses key-value pairs for storage instead of documents.
Install MySQLTo install MySQL on a Linux distribution running on WSL, just follow the Installing MySQL on Linux instructions in the MySQL docs. You may need to first enable systemd support in your wsl.conf
configuration file.
Example using the Ubuntu distribution:
sudo apt update
sudo apt install mysql-server
mysql --version
systemctl status mysql
sudo mysql
SHOW DATABASES;
CREATE DATABASE database_name;
DROP DATABASE database_name;
For more about working with MySQL databases, see the MySQL docs.
To work with MySQL databases in VS Code, try the MySQL extension.
You may also want to run the included security script. This changes some of the less secure default options for things like remote root logins and sample users. This script also includes steps to change password for MySQL root user. To run the security script:
sudo service mysql start
sudo mysql_secure_installation
To install PostgreSQL on WSL (ie. Ubuntu):
sudo apt update
sudo apt install postgresql postgresql-contrib
psql --version
There are 3 commands you need to know once PostgreSQL is installed:
sudo service postgresql status
for checking the status of your database.sudo service postgresql start
to start running your database.sudo service postgresql stop
to stop running your database.The default admin user, postgres
, needs a password assigned in order to connect to a database. To set a password:
sudo passwd postgres
To run PostgreSQL with psql shell:
sudo service postgresql start
sudo -u postgres psql
Once you have successfully entered the psql shell, you will see your command line change to look like this: postgres=#
Note
Alternatively, you can open the psql shell by switching to the postgres user with: su - postgres
and then entering the command: psql
.
To exit postgres=# enter: \q
or use the shortcut key: Ctrl+D
To see what user accounts have been created on your PostgreSQL installation, use from your WSL terminal: psql --command="\du"
...or just \du
if you have the psql shell open. This command will display columns: Account User Name, List of Roles Attributes, and Member of role group(s). To exit back to the command line, enter: q
.
For more about working with PostgreSQL databases, see the PostgreSQL docs.
To work with PostgreSQL databases in VS Code, try the PostgreSQL extension.
Install MongoDBTo install MongoDB, see the Mongodb docs: Install MongoDB Community Edition on Linux
Installing MongoDB may require slightly different steps depending on the Linux distribution being used for installation. Also note that MongoDB installation may differ depending on the version # that you are aiming to install. Use the version drop-down list in the top-left corner of the MongoDB documentation to select the version that aligns with your goal. Lastly, you may need to enable systemd support in the wsl.conf
configuration file of the Linux distribution that you are using with WSL. The systemctl
command is a part of the systemd init system and may not work if your distribution is using systemv.
VS Code supports working with MongoDB databases via the Azure CosmosDB extension, you can create, manage and query MongoDB databases from within VS Code. To learn more, visit the VS Code docs: Working with MongoDB.
Learn more in the MongoDB docs:
Install Microsoft SQL ServerQuickstart: Install SQL Server and create a database on Windows Subsystem for Linux (WSL 2).
Install SQLiteTo install SQLite on WSL (ie. Ubuntu):
sudo apt update
sudo apt install sqlite3
sqlite3 --version
To create a test database, called "example.db", enter: sqlite3 example.db
To see a list of your SQLite databases, enter: .databases
To see the status of your database, enter: .dbinfo ?DB?
Database will be empty after creation. You can create a new table for your database with CREATE TABLE empty (kol INTEGER);
.
Now entering the .dbinfo ?DB?
will show the database you have created.
To exit the SQLite prompt, enter: .exit
For more information about working with a SQLite database, see the SQLite docs.
To work with SQLite databases in VS Code, try the SQLite extension.
Install RedisTo install Redis on WSL (ie. Ubuntu):
sudo apt update
sudo apt install redis-server
redis-server --version
To start running your Redis server: sudo service redis-server start
Check to see if redis is working (redis-cli is the command line interface utility to talk with Redis): redis-cli ping
this should return a reply of "PONG".
To stop running your Redis server: sudo service redis-server stop
For more information about working with a Redis database, see the Redis docs.
To work with Redis databases in VS Code, try the Redis extension.
See services running and set up profile aliasesTo see the services that you currently have running on your WSL distribution, enter: service --status-all
Typing out sudo service mongodb start
or sudo service postgres start
and sudo -u postgrest psql
can get tedious. However, you could consider setting up aliases in your .profile
file on WSL to make these commands quicker to use and easier to remember.
To set up your own custom alias, or shortcut, for executing these commands:
Open your WSL terminal and enter cd ~
to be sure you're in the root directory.
Open the .profile
file, which controls the settings for your terminal, with the terminal text editor, Nano: sudo nano .profile
At the bottom of the file (don't change the # set PATH
settings), add the following:
# My Aliases
alias start-pg='sudo service postgresql start'
alias run-pg='sudo -u postgres psql'
This will allow you to enter start-pg
to start running the postgresql service and run-pg
to open the psql shell. You can change start-pg
and run-pg
to whatever names you want, just be careful not to overwrite a command that postgres already uses!
Once you've added your new aliases, exit the Nano text editor using Ctrl+X -- select Y
(Yes) when prompted to save and Enter (leaving the file name as .profile
).
Close and re-open your WSL terminal, then try your new alias commands.
Ensure that you are running your Linux distribution in WSL 2 mode. For help switching from WSL 1 to WSL 2, see Set your distribution version to WSL 1 or WSL 2.
Additional resourcesRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3