9

I just installed Postgres on Ubuntu 20 on WSL. Both of the following are stuck forever:

CREATE DATABASE foo
WITH
OWNER = postgres      
ENCODING = 'UTF8'     
CONNECTION LIMIT = -1;

And:

sudo -u postgres createdb foo

The log doesn't have anything:

postgres@postgres ERROR:  canceling statement due to user request
postgres@postgres STATEMENT:  CREATE DATABASE foo
            WITH
            OWNER = postgres
            ENCODING = 'UTF8'
            CONNECTION LIMIT = -1;

What are some possible causes for this?

Restarting Postgres or restarting the computer doesn't help and there's no locks. I also tried manually killing every lock.

5
  • Please share more details - is this even related to programming? Commented Nov 2, 2021 at 10:30
  • 1
    What kind of details? I installed Postgres, then the next step is creating a database, but it's hanging Commented Nov 2, 2021 at 10:38
  • 1
    If this is a pure problem with Postgres, it might be better suited at serverfault.com or dba.stackexchange.com Commented Nov 2, 2021 at 10:50
  • @LeoJiang Have you found an answer? I'm also getting this behaviour in WSL1 with PostgreSQL 14 (13 works fine). I don't want to switch to WSL2. Commented Mar 2, 2022 at 21:23
  • I don't remember, probably just deleted the data directory and reinstalled Commented Mar 3, 2022 at 11:26

4 Answers 4

6

I had the same issue with you. It seems that it has to do with the wsl version. I was running wsl 1 so I had to uninstall ubuntu 20.04 wsl, then set the verion to 2 with the following command

wsl --set-default-version 2

and then install ubuntu 20.04 wsl again. Now everything seems to be in order and I am able to create databases with no issues.

Sign up to request clarification or add additional context in comments.

2 Comments

I had to use the command wsl --set-default-version *distribution_name* 2 elsewhise it did not work on my computer. With the command wsl -l -v one can see which distribution is installed with which wsl version.
yes wsl 2 will not work for me see this error message. C:\Users\rlc>wsl --set-version Ubuntu-22.04 2 For information on key differences with WSL 2 please visit aka.ms/wsl2 Conversion in progress, this may take a few minutes. The operation could not be started because a required feature is not installed. Error code: Wsl/Service/CreateVm/HCS_E_SERVICE_NOT_AVAILABLE I have other virtual machines and wsl 2 is not possible. I need a wsl 1 solution.
3

There is a work around: Check you distros version by:

wsl -l -v

update the wsl version of you distros to 2 and then create the database it will get created.

Also other work arounds:

if you need to update the ubuntu switch back to wsl 1 and then update the ubuntu

sudo apt update
sudo apt upgrade 

after doing it switch back to wsl2

To update or degrade use the following commands:

Move to version 2:

wsl --set-version Ubuntu-22.04 2

Back to version 1:

wsl --set-version Ubuntu-22.04 1

2 Comments

I am not sure why your answer got downvoted. Actually one can change WSL version without destroying the whole distro, as you say. The conversion can take about 5 minutes but it's fine. It saves a lot of time that would be otherwise spend on installing & configuring everything again. So, thank you for this answer, it should get upvoted more.
@WojciechKaczmarek this is the trick we can apply as a work around, Thanks for support.
2

While the statement is hanging, connect to the postgres database and look in pg_stat_activity for the wait_event and wait_event_type of the hanging connection.

Very likely it is waiting for a lock. In that case, end the transaction that is holding the lock.

Comments

2

take a look of

select * from pg_stat_activity sa inner join pg_locks l on sa.pid = l.pid

In my case I use wsl and get lock by AccessShareLock
its more a problem with wsl than postgresql

2 Comments

But how do you solve it? I seem to have the same issue as well
I stop using wsl!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.